Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Locale specific date without year

I am using https://github.com/abritinthebay/datejs/ for date formatting due to locale support. However, is it not possible to get a full date time without year?

Example

Input date:2014/09/20 20:00:00

Output date: 09/20 20:00

And it has to respect locale settings!

like image 657
Esben Avatar asked Jul 27 '26 08:07

Esben


1 Answers

Looks like since ES2015 you can just skip first parameter and set only 'options' parameter, in that way locale will be applied:

new Date().toLocaleString(undefined, {
    month: "short", day: "numeric", 
    hour: "numeric", minute: "numeric", second: "numeric"
}) // "Jul 11, 5:50:09 PM"

I didn't find the way to remove comma between date and time. For that case string formatting can be used:

const dateTime = new Date();
const datePart = dateTime.toLocaleDateString(undefined, {month: "short", day: "numeric"});
const timePart = dateTime.toLocaleTimeString();
const result = `${datePart} ${timePart}`;
// "Jul 11 5:57:10 PM"
like image 50
Egor Popov Avatar answered Jul 29 '26 21:07

Egor Popov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!