Coder Perfect

How to format a JavaScript date


How do I format a date object in JavaScript to print as 10-Aug-2010?

Asked by leora

Solution #1

Date#toLocaleDateString can be used to construct standard locale-specific renderings if you need slightly less control over formatting than the currently accepted answer. The locale and options arguments allow applications to define which language’s formatting norms should be used, as well as some rendering customisation.

All of these keys are completely optional. Depending on your needs, you can vary the number of possibilities values, which will also represent the presence of each date time phrase.

Note that supplying null for the first parameter will result in an error if you only want to configure the content choices and keep the existing locale. Instead, use undefined.

There are more language options available to you.

For the same purpose, you can utilize the toLocaleString() method. The only difference is that this function calculates the time when no options are passed.

// Example
9/17/2016, 1:21:34 PM

Answered by ajeet kanojia

Solution #2

To build custom-delimited date formats, extract the date (or time) components from a DateTimeFormat object (part of the ECMAScript Internationalization API) and manually create a string with the desired delimiters.

You may do this with DateTimeFormat#formatToParts. You could destructure the array, but this isn’t ideal because the array output is locale dependent.

It would be preferable to translate a format array to the strings that result:

You can also use DateTimeFormat#format to extract individual elements of a DateTimeFormat; however, as of March 2020, there is a flaw in the ECMAScript implementation when it comes to leading zeros on minutes and seconds when using this method (this bug is circumvented by the approach above).

Because of the many underlying intricacies of the field, it is usually worth using a library (e.g. moment.js, luxon) when working with dates and times.

The ECMAScript Internationalization API, which is utilized in the solutions above, is not supported in Internet Explorer 10. (0.03 percent global browser market share in Feb 2020).

Answered by Marko

Solution #3

Use getDate, getMonth + 1, getFullYear, getHours, and getMinutes in plain JavaScript to rapidly prepare your date:

var d = new Date();

var datestring = d.getDate()  + "-" + (d.getMonth()+1) + "-" + d.getFullYear() + " " +
d.getHours() + ":" + d.getMinutes();

// 16-5-2015 9:50

If you need it to be padded with zeros, use this formula:

var datestring = ("0" + d.getDate()).slice(-2) + "-" + ("0"+(d.getMonth()+1)).slice(-2) + "-" +
    d.getFullYear() + " " + ("0" + d.getHours()).slice(-2) + ":" + ("0" + d.getMinutes()).slice(-2);

// 16-05-2015 09:50

Answered by sebastian.i

Solution #4

Use the date.format library to create the following:

var dateFormat = require('dateformat');
var now = new Date();
dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT");


Saturday, June 9th, 2007, 5:46:21 PM 

dateformat on npm

Answered by RobertPitt

Solution #5

So, my goal was to convert today’s date to a MySQL-friendly date string, such as 2012-06-23, and utilize that string as a parameter in one of my queries. This is the simple solution I’ve discovered:

var today = new Date().toISOString().slice(0, 10);

Remember that the solution above does not account for your timezone offset.

Instead, you might use the following function:

function toJSONLocal (date) {
    var local = new Date(date);
    local.setMinutes(date.getMinutes() - date.getTimezoneOffset());
    return local.toJSON().slice(0, 10);

If you run this code towards the start or end of the day, this will give you the right date.

Answered by simo

Post is based on