JavaScript Date Formats

By | April 12, 2021
JavaScript Date Formats

In the previous JavaScript tutorial, we learned about the Date() object in JavaScript and how to create them using new Date() constructor. There we also learned that we can use the string date type and convert it into a Date object. In this tutorial, we will learn what are the different date formats we can use as a string, to convert it into a Date() object.

Date format

In javascript, we can represent string data in 3 different formats.

Vamware
Type Format Example
ISO “YYYY-MM-DD” “2021-12-30”
Short “MM/DD/YYYY” “12/30/2021”
Long “Month DD YYYY ” or “DD Month YYYY” “Dec 30 2021” or “30 Dec 2021”

JS ISO Date

In ISO ( International Organization for Standardization) format, we represent the Date in (YYYY-MM-DD) format.

Example 

let iso_date = "2021-12-30";
let date = new Date(iso_date );
console.log(date);   //Thu Dec 30 2021 05:30:00 GMT+0530 (India Standard Time)

By default the Date is converted into user browser-specific timezone, in my case, it converted into India Standard Time.

ISO Date with Year and Month

We can also specify only Year and Month data and create a Date object with (YYYY-MM) format only, and the rest of the details like time day, etc will be filled automatically.

Example

let iso_date = "2021-12"
let date = new Date(iso_date );

console.log(date);    //Wed Dec 01 2021 05:30:00 GMT+0530 (India Standard Time)

As you can see that the time (05:30:00) and day 01 automatically assigned to the date object when we only specify the year and month.

ISO Date with Year

We can also only pass the Year data and the Date() object will set the rest of the data automatically.

Example

let iso_date = "2021";     // YYYY
date = new Date(iso_date );
console.log(date)      //Fri Jan 01 2021 05:30:00 GMT+0530 (India Standard Time)

JS ISO Date with date & time

We can also specify the compete date and time to create a Date object. To create a complete manual date-time object we can use the following format (YYYY-MM_DDTHH:MM:SSZ).

Example

let iso_date = "2021-12-30T12:00:00"
let datetime = new Date(iso_date );
console.log(datetime); // Thu Dec 30 2021 12:00:00 GMT+0530 (India Standard Time)

Here the T represents the separator between date and time. And Z represents the UTC timezone(omit Z it will follow your browser timezone).

JS Short Dates

In the short Date, we represent the dat2 in the following format “MM/DD/YYYY”

Example

let short_date = "12/30/2021";
let date = new Date(short_date);
console.log(date);    // Thu Dec 30 2021 00:00:00 GMT+0530 (India Standard Time)

Some browser throw an error if we do not specify the leading 0 before the data ex "23-3-2021"so its alwayse a good practice to use leading zero to represent single digit date “23-03-2021“.

JS Long Dates

JS Date() object also supports the Long date format. To write the date in long date format we can use the following format “MMM DD YYYY”.

Example

let long_date_1 = "Dec 30 2021"; //"MMM DD YYYY"
let long_date_2 = "30 Dec 2021"; // DD MMM YYYY
let long_date_3 = "December 30 2021"; // MMMMMM DD YYYY
date1 = new Date(long_date_1);
date2 = new Date(long_date_2);
date3 = new Date(long_date_3);
console.log(date1);    // Thu Dec 30 2021 00:00:00 GMT+0530 (India Standard Time
console.log(date2);     //Thu Dec 30 2021 00:00:00 GMT+0530 (India Standard Time
console.log(date3);   //Thu Dec 30 2021 00:00:00 GMT+0530 (India Standard Time)

Parse Date into Milliseconds

We can convert a valid date into milliseconds. The Date object comes with an inbuilt parse() method which converts a valid string date into milliseconds from date 1 January 1970.

Example

let current_iso_date ="March 21 2021";
let millisec = Date.parse(current_iso_date);  
console.log(millisec);    //1616265000000

Then we can use the milliseconds to convert back into Date.

let millisec = 1616265000000;
date = new Date(millisec);
console.log(date); //Sun Mar 21 2021 00:00:00 GMT+0530 (India Standard Time)

Summary

  • In JavaScript, we can represent string date in 3 formats ISO, short and Long.
  • In ISO we represent date using the “YYYY-MM-DD” format.
  • In Short Date, we represent the date using “MM/DD/YYYY” format.
  • In Long date, we represent date using “MMMM DD YYYY” format.

Leave a Reply

Your email address will not be published. Required fields are marked *