You-Dont-Need-Momentjs icon indicating copy to clipboard operation
You-Dont-Need-Momentjs copied to clipboard

Native Date format using toLocaleDateString

Open jecovier opened this issue 5 years ago • 5 comments

Hi, I really like this! thanks for your effort!

I Know it's not exactly the same, but i think you can add toLocaleDateString as an native option for date formatting. More in the link below:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString

jecovier avatar Nov 20 '18 20:11 jecovier

PR welcome

stevemao avatar Nov 20 '18 22:11 stevemao

Well, for the examples in the table, I think all you need is:

var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric' };
date.toLocaleDateString('en-US', options);

and

var options = { weekday: 'short', hour: 'numeric'};
date.toLocaleDateString('en-US', options);

You can also put undefined as the first parameter if you want to be locale-neutral; would probably be worth a comment

Amorymeltzer avatar Oct 27 '19 15:10 Amorymeltzer

I can make the simple PR, but tests fail because there's a comma difference: Sunday, September 9, 2018 in native Sunday, September 9th 2018 in moment

Native also doesn't support nth

Amorymeltzer avatar Oct 27 '19 16:10 Amorymeltzer

@Amorymeltzer feel free to fix the test too.

stevemao avatar Oct 27 '19 22:10 stevemao

How about Intl.DateTimeFormat? Performance note as mentioned in the toLocaleDateString mdn page.

aesyondu avatar Feb 09 '20 11:02 aesyondu