JavaScript Date Format – How to Format a Date in JS

JavaScript Date Format

JavaScript Date objects represent one and a single moment in time in a platform-independent format. Date objects summarize an integral number that represents milliseconds since midnight at the beginning of January 1, 1970, UTC (the epoch). A JavaScript date is specified as the number of milliseconds that have passed since the ECMAScript epoch, which is further defined as the midnight at the beginning of January 1, 1970, UTC.

There are generally three types of JavaScript date formats:

Short Date – 17.17.2020

Long Date – Jan 17 2023 or 17 Jan 2023

ISO Date – 2023-01-17 ( International Standard )

A strict standard is followed by ISO format in JavaScript. The other formats are not so well defined and encouraged to be used as they might be browser specific.

By default, independent of the platforms, JavaScript will output dates in full string format, For example:

Tue Jan 17 2023 12:47:13 GMT+0530 (India Standard Time)

ISO DATES – 

const date = new Date(“2023-01-17”);

ISO DATES ( only year ) –

const date = new Date(“2023”);

ISO DATES ( date and time )

const date = new Date(“2023-01-17T12:00:00Z”);

The date and time are separated with the capital letter T.UTC time is defined with the capital letter Z. If we want to modify the time relative to UTC,  we can remove Z and add +HH:MM or -HH:MM instead. For example –

const date = new Date(“2023-01-17T12:00:00-06:30”);

Methods of Conversion of Dates

In addition to methods to read and modify or alter the individual components of the local date and time (such as getDay() and setHours()), there also exists versions of the same methods that read and manipulate or modify the date and time using UTC (such as getUTCDay() and setUTCHours()).

Tags: No tags

Add a Comment

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