0

EDIT: I think it's crazy that JavaScript can talk to USB devices but can't simply pull the OS' preferred date format. Doing more research, this question is a duplicate of Javascript - Retrieve OS Date formatting. Thanks to everybody who tried to help anyway.

How do you format a date to the user's system preferred format? Previous research I have done indicates that getLocaleDateString() will do this, however, I am still seeing incorrect behavior.

Browser's locale is set to "en_US". OS' date format is is set to 2020-03-11. When I call (new Date()).getLocaleDateString(), I get 3/11/2020, which is not the format I'd expect. I cannot seem to find any JavaScript methods that will format to a user's preferred date format, nor can I find any settings in my browser to alter this format either. I feel like I must be doing something wrong because it seems weird that application developers would responsible for date formatting for the user.

Ryan
  • 51
  • 3
  • with your choice of numbers its not clear what is the month/day... could you update with 31/12/2020 as your example date – dmoo Mar 11 '20 at 14:50
  • 3
    you can use `moment.js` to format dates or you can do it all yourself manually – Prakash Reddy Potlapadu Mar 11 '20 at 14:51
  • 3
    As usual with dates manipulations, [moment.js](https://momentjs.com/) will save you a few headaches. – Jeremy Thille Mar 11 '20 at 14:52
  • See this answer - https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date – dmoo Mar 11 '20 at 14:53
  • Please, for the love of everything good, get [moment.js](https://momentjs.com/) –  Mar 11 '20 at 14:56
  • 1
    Using moment.js does not answer this question, nor does https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date. Thanks though. – Ryan Mar 11 '20 at 15:12

3 Answers3

3

The correct answer (at least today) seems to be that this is not possible. I have found this duplicate question that has no answers on it: Javascript - Retrieve OS Date formatting.

If anybody knows the actual answer to this question, answer this other question instead.

Ryan
  • 51
  • 3
0

You can add parameters to the .toLocalString() method.

en-us format: MM/dd/yy h:mm a
new Date().toLocaleString('en-us', { timeZone: 'UTC' })
returns "3/11/2020, 3:02:21 PM"
en-gb format: DD/MM/YYY HH:MM
new Date().toLocaleString('en-gb', { timeZone: 'UTC' })
returns "11/03/2020, 15:02:26"

BTW: .toLocalString() looks at the browsers settings, not the OS's.

Lumpenstein
  • 1,250
  • 1
  • 10
  • 27
  • 1
    The question clearly states independent of language/region. – Ryan Mar 11 '20 at 15:13
  • For the date format browser settings you are looking for, it is the language of the browser/preffered website language that is determinating the date format. – Lumpenstein Mar 11 '20 at 15:16
-1

If you prefer not using momentjs and do it yourself,You can extract individual values like this and format into any format you want

const dateValue = new Date(dateString);
const dd = dateValue.getDate();
const MM = dateValue.getMonth() + 1;
const yyyy = dateValue.getFullYear();

For example to convert today's date to mm/dd/yyyy format

const date=new Date();
console.log(date);

const formatDate=(dateString,requiredFormat)=>{
const dd=dateString.getDate();
const mm=dateString.getMonth()+1;
const yyyy=dateString.getFullYear();
 if(requiredFormat.toLowerCase()==='mm/dd/yyyy')
   return `${mm}/${dd}/${yyyy}`
}

console.log(formatDate(date,'mm/dd/yyyy'));