0

I know this question have been often asked, but I havn't found a way to format a date string to full text without any library.

Example: "2020-10-23T13:00:48.207Z" => "October, 23rd 2020".

It would bother me to install a full date library just for a simple string. Also, is it possible to get this date in any language ("23 Octobre 2020" for exemple)?

DoneDeal0
  • 1,493
  • 1
  • 7
  • 24
  • I think you'll need a library lol :D – desbest Dec 16 '20 at 20:03
  • You need a library to format it easily, or do it by hand. Look at the top answer here https://stackoverflow.com/questions/31089749/how-do-you-set-a-strftime-with-javascript – Yotam Salmon Dec 16 '20 at 20:05
  • I'd be ok with an open-source library, that would allow me to copy-paste the small portion of code needed to achieve this. If someone knows such a file in Luxon, moment and whatnot, this would be highly appreciated! – DoneDeal0 Dec 16 '20 at 20:06
  • @Badashi answered correctly below. – DoneDeal0 Dec 16 '20 at 20:23

1 Answers1

0

"Without any library" is a though call without knowing your environment, but most browsers support Intl.DateTimeFormat

console.log(
  new Intl.DateTimeFormat('en-US', { dateStyle: 'full' }).format(new Date())
// => "Wednesday, December 16, 2020"
);
RobG
  • 124,520
  • 28
  • 153
  • 188
Badashi
  • 976
  • 7
  • 16
  • You're the boss!!! Of course, IE and Safari - as expected - don't support datestyle feature, but it's fine because it will render a MM/DD/YYYY format. Also, while testing this, I've just discovered that Safari doesn't support underscore separation in big numbers such as 234_765_654. It will throw an error, so if you work with milliseconds please note that. Thank you so much Badashi! – DoneDeal0 Dec 16 '20 at 20:22
  • For me this returns a timestamp like "12/17/2020". Formatting with the Intl object is not standardised, so shouldn't really be used where a specific format is required except perhaps in conjunction wth the *formatToParts* method and manual formatting. – RobG Dec 16 '20 at 23:22