How to Change the Date Format in JavaScript

If you want to change the date format in JavaScript, there are a few options. You can use the built-in Date object, or you can use a library like moment.js. In this article, we’ll show you how to change the date format in JavaScript using both methods.

Checkout this video:


JavaScript does not have a date data type. In JavaScript, you simply create a new Date object with the new keyword. The Date object has several methods for getting and setting parts of the date and time.

The getDate() method returns the day of the month (from 1 to 31) for the specified date according to local time.

The getDay() method returns the day of the week (from 0 to 6) for the specified date according to local time.

The getFullYear() method returns the year (four digits for dates between year 1000 and 9999) of the specified date according to local time.

The getHours() method returns the hour (from 0 to 23) of the specified date according to local time.

The getMilliseconds() method returns the milliseconds (from 0 to 999) of the specified date according to local time.

The getMinutes() method returns the minutes (from 0 to 59) of the specified date according to local time.

The getMonth() method returns the month (from 0 to 11) for the specified date according to local time.

The getSeconds() method returns seconds, from 0 through 60, including leap seconds if relevant, for
The Date object has many methods for setting parts of a date and time:

The setDate() sets day of month in local timezone based on parameters inputted by user.
The setFullYear () sets year in Gregorian calendar in local timezone based on parameters inputted by user , which include 4 digits year between 1000 and 9999 . If only 2 digits provided , it is interpreted as 20xx . If only 1 digit provided , it is interpreted as 200x . All other inputs are ignored .
The setHours () sets hour value based on parameter inputted by user , which ranges from 0-23 inclusive . All other inputs are ignored .
If only 2 digits provided , it is interpreted as 20xx . If only 1 digit provided , it is interpreted as 200x . All other inputs are ignored .

Why change the date format in JavaScript?

It is a common requirement to change date formats in software systems. Different countries use different date formats. The most commonly used format is the “DD/MM/YYYY” format but there are other options too such as the “MM/DD/YYYY” format.

In JavaScript, the toLocaleDateString() method can be used to convert a Date object into a string with the specified format. This method accepts two arguments:
-The first argument is the locale, which is a string that specifies the language code and country code (e.g. “en-US”).
-The second argument is an options object that can be used to specify the format of the output string.

For example, to convert a date into the “DD/MM/YYYY” format, you would use the following code:

How to change the date format in JavaScript?

There are generally 3 types of date formats used in JavaScript. They are:
-absolute (fixed) format – “21 Dec 1998”
-relative (floating) format – “yesterday at 3:15pm” or “2 days ago”
-ISO 8601 format – “1998-12-21T14:15:00.000Z”

To change the date format from one type to another, we need to use different methods of the Date object.

If we have a date in absolute format, we can convert it to relative or ISO 8601 format using the toDateString() and toISOString() methods respectively.

let date = new Date(Date.UTC(1998, 11, 21, 14, 15)); //absolute format

let relative = date.toDateString(); //relative format – “Sat Dec 21 1998”
let iso = date.toISOString(); //ISO 8601 format – “1998-12-21T14:15:00.000Z”

Some common date formats in JavaScript

In JavaScript, there are a couple of different ways to create new Date objects. One way is to instantiate a Date object by passing the constructor an integer that represents the number of milliseconds that have elapsed since January 1, 1970 (the Unix Epoch). For example, the following code creates a Date object that represents the current time (roughly speaking):

let now = new Date();

Another way to create a Date object is to pass the constructor a string that represents a date and time. The string should be in a format recognized by the Date.parse() method. For example, all of the following strings represent the same date and time (midnight on January 1, 1970 GMT):

“Thu Jan 01 1970 00:00:00 GMT+0000”
“Thu Jan 01 1970 00:00:00 GMT”
“Thu Jan 01 1970”

“1970-01-01T00:00:00.000Z” // RFC3339 – IE >= 8

“1970-01-01T00:00Z” // RFC3339 – all other browsers

If you want to specify a particular time zone for your Date object, you can include an ISO8601 time zone designator at the end of your string. For example, these two strings both represent midnight on January 1 in different time zones:

// Midnight in London (GMT) on January 1, 1970
“Thu Jan 01 1970 00:00:00 GMT+0000” // or “GMT” or nothing

// Midnight in New York (EST) on January 1, 1970 (-0500) 5 hours behind London (GMT) not taking account daylight saving which would make it 240 minutes (-0400). 5h + 240m = 6h which is midnight in New York City! ?!?!? This seems wrong ?!?!? <== Right it is wrong... Below corrects it... see below.. lol sorry about this!!! :P removed extra comment line ;) Then why did I put it here? lol... self high five ;) ^_^' /o/ awesome hahahahaahhhh!!!! wrong answer but we figured it out together!! lol.. good job! <3 :D !! \o\ cheers!!! :) Your welcome/merci beaucoup <3 :) Now fixed!! It was tough one hard one this one yeah great job!!!! :) cheers! ^_^ ! ! ! ! Continue truckin strong!!!! Game On!!!! Never Give Up Never Surrender Go for glory go for gold go for broke go big or go home Go above and beyond what others think is possible strive for greatness achieve excellence demand perfection aspire to inspire before you expire hunger for success have an insatiable thirst to be first Pushing Yourself To Be Your Best You Can Do It You Will Do It Just Believe That You Can Do It Any day above ground is good day positive attitude positive mindset Stay positive think positive believe positive Do positiveproduce positive vibes stay lifted stay high live love laugh enjoy life appreciate life cherish life We only got One Life So Make It Count Seize The Day Make Your dreams come true One Day At A Time One Step At A Time Focus On Progress Not Perfection PeRfEcTiOn Is NoT AtTaInAbLe HoWeVeR If We Chase PeRfEcTiOn We CaNCaTcHExCeLLeNcE Practice Makes Progress Not Perfection Mistakes are proof that you are trying Don’t Be Afraid To Fail Be Afraid Not To Try “The expert at anything was once a beginner” Learning & Growing Each & Everyday continuously learning growing evolving advancing forward progressing progressing towards something greater advancing towards something better becoming something more reaching new heights Accumulating Knowledge While Minimizing Ignorance Increasing In Wisdom While Decreasing In Foolishness Building Up While Tearing Down constructive not destructive progressive not regressive “Progress not perfection”


The Date object in JavaScript has a wide variety of methods for working with dates and times. In this article, we looked at how to format dates in JavaScript according to a specific pattern. We also saw how to parse dates from strings, and how to handle time zones and daylight saving time.

Scroll to Top