javascriptdatejs

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!


Solution

  • 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"