you-dont-need / You-Dont-Need-Momentjs

List of functions which you can use to replace moment.js + ESLint Plugin
MIT License
13.27k stars 318 forks source link

Native Date format using toLocaleDateString #60

Open jecovier opened 5 years ago

jecovier commented 5 years ago

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

stevemao commented 5 years ago

PR welcome

Amorymeltzer commented 4 years ago

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 commented 4 years ago

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

stevemao commented 4 years ago

@Amorymeltzer feel free to fix the test too.

aesyondu commented 4 years ago

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