I want to know how to convert a long date like this 1542814586896 into a String format like this 2019/02/05
2 Answers
You can use Date class for setting time in integer format and getting any values like day, month, year
let date = new Date(1542814586896);
console.log(date.getDate(), date.getMonth(), date.getFullYear())

- 284
- 1
- 8
-
1getDay() means week and getDate() means day of the month – AbdusSalam Jun 12 '23 at 03:35
-
1@AbdusSalam thanks, you've right, I've fixed it! – Pavel Shirobok Jun 15 '23 at 04:41
You can use
new Date(1542814586896).toLocaleDateString(`ja-JP`);
//-> "2018/11/21"
.toLocaleDateString() formats time into a format of a specific region. In the example above, time's formatted into Japanese format (just because it seems like in Japan they use exactly the format you need).
What's cool about this method is that you may just pass no argument to toLocaleDateString & it will then just automatically pick the format that the final user prefers (or more precisely, the format that is set in user's OS).
For example in my browser:
new Date(1542814586896).toLocaleDateString();
//-> "21/11/2018"
However, if I had Egyptian Arabic set as main language of my operating system, the result should be like:
new Date(1542814586896).toLocaleDateString();
//-> "٢١/١١/٢٠١٨"
You may find more information about different locales & corresponding formats here.

- 2,532
- 3
- 11
- 19
-
I think this highlights the poor choice of *toLocaleString* as the primary means of formatting ECMAScript dates. One must go looking for a language code that will produce the required result, even if it has nothing to do with the language being used. BTW, the language code "ja" is sufficient, the only sub–code is "JP". :-) – RobG May 05 '19 at 11:34
-
@RobG I'd say that it's a great tool if you care about final users' comfort rather than combinations of dots & dashes in text on the site. I rather see a problem in the fact that developers & designers mostly tend to care exactly about dots & dashes. From my point of view I'd always prefer to show people things in the format they comfortable with instead of format I visually like. – Igor Bykov May 05 '19 at 15:57
-
Yet your advice is to select a particular language code because it produces the required format. To me that's no different to, and less reliable than, using, say, moment.js and `momentDate.format('YYYY/MM/DD')`. I care about users not being confused so recommend unambiguous formats like "DD-MMM-YYYY" or "D MMMM, YYYY". It's far better to be clearly understood than attempt to find a "comfortable format" when I have no idea what that might be for a particular user, especially on the web. – RobG May 06 '19 at 06:29
-
My *answer* to this question is to select a language code that provides the needed format (since that is what was asked). My *advice* is to use toLocale*() methods without passing an argument. So, it'll pick up whatever format that final user is comfortable with without any need to hard-code the format. – Igor Bykov May 06 '19 at 06:40
-
If given no language code, *toLocaleString* may produce unpredictable results. E.g. on my system, Firefox defaults to m/d/y, Chrome to d/m/y. – RobG May 06 '19 at 08:03