Signup/Sign In

JavaScript Date Object

JavaScript Date object is a built-in object which is used to deal with date and time. It stores a Number which represents milliseconds for the Unix Timestamp(which is nothing but milliseconds passed since 1 January 1970 UTC). We can use it to create a new date, format a date, get elapsed time between two different time values, etc.

It also helps to get a date value using milliseconds number and we can get a date based on the different timezones as well.

The Date object use browser's date timezone by default and display date in the text format.

The Date object can be created using the Date constructor function.

Creating the JavaScript Date Object

Let's see how we can create the Date object in JavaScript. Although there are multiple ways of doing it, the default way is using the new keyword with the Date constructor function.

let newdate = new Date();

The above will create a new Date object with the current date and time stored in it. The format of the date stored in this object will be:


Tue May 05 2020 21:16:17 GMT+0530 (India Standard Time)

Yes, it will be the complete date and time along with timezone information.

To get the standard UNIX timestamp value we can use the following code:

let newdate = Date.now();


1588693792007

In the code above we have used the static function now() for the Date object. This will give us the Unix timestamp for current date and time which is nothing but the number of milliseconds passed since 1 January 1970.

Other Ways to Create JavaScript Date

Apart from the above two syntaxes, there are multiple other ways to create a date in JavaScript. Here, we are using different syntax to create date objects.

As you can see in the examples above, we can create a date for a custom date which can be a past date or a future date. We can even provide the time value.

Working with JavaScript Date

JavaScript Date constructor handles different formats to create a date, we can pass a string format date or a numerical literal with milliseconds to create a date object in JavaScript.

Creating JavaScript Date using String

JavaScript allows the following string formats to create a date object.

let date1 = new Date('February 13, 1991 06:44:00')
document.write(date1 +"<br>");

let date2 = new Date('1991-02-13T06:44:00');
document.write(date2);


Wed Feb 13 1991 06:44:00 GMT+0530 (India Standard Time)
Wed Feb 13 1991 06:44:00 GMT+0530 (India Standard Time)

Creating JavaScript Date using Numerical Literal

We can use the Date() constructor can take 7 arguments to create a date, which is: year, month, day, hour, minute, second, and millisecond.

We can also get a date by just passing milliseconds too. See the below example:

let date1 = new Date(2018,11,25,12,25,12,0);
document.write(date1 +"<br/>");

// Passing miliseconds
let date2 = new Date(1825356800000);
document.write(date2);


Tue Dec 25 2018 12:25:12 GMT+0530 (India Standard Time)
Fri Nov 05 2027 01:03:20 GMT+0530 (India Standard Time)

Get Year, Month and Day from a JavaScript Date

To get the value for the year, month or day from a date, we can use JavaScript built-in methods. These methods are easy to use and reduce the effort and time for coding.

// Creating a date
let date1 = new Date(2018,11,25,12,25,12,0);

// Getting year
let year = date1.getFullYear();
document.write("<br> Year: "+  year);

// Getting Month
let month = date1.getMonth();
document.write("<br> Month: "+  month);

// Getting day
let day = date1.getDate();
document.write("<br> Day: "+  day);


Year: 2018
Month: 11
Day: 25

JavaScript Date Object Methods

JavaScript provides a rich set of built-in methods which are both static and instance methods that can be used to get different information from a JavaScript Date object.

Method Description
Date.now() Returns the number of milliseconds(Unix timestamp) for the current date and time.
Date.parse() Parses a string value of date and returns the number of milliseconds since 1 January 1970, 00:00:00 UTC, with leap seconds ignored.
Date.UTC() This function takes 7 parameters with numeric values but creates the date in UTC timezone.
setDate() sets the date of the month that ranges from 1 to 31.
setHours() set hours that range from 0 to 23
getSeconds() returns the seconds that range from 0 to 59
toDateString() converts a date value into a string
valueOf()

returns the primitive value of Date object.

setMinutes() set minutes that range from 0 to 59.
setMonth() set numerical equivalence of month range from 0 to 11
setMilliseconds() set the milliseconds that range from 0 to 999
toTimeString()

converts time into a string.

getDate()

returns the day of the month from 1 - 31

getDay()

returns the day of the week from 0 - 6

getFullYear()

returns the year (4 digits for 4-digit years) of the specified date

getMinutes()

returns the minutes (059) in the specified date

getMonth()

returns the month (011) in the specified date

getHours() returns the hours that that range from 0 to 23.

Now we are going to show you how to use some of the Date Object methods:

Finding the difference between JavaScript Date - Elapsed Time

We can simply subtract two dates to find the difference between any two dates. Let's take an example to see this:

// Using Date objects
let start = Date.now();

// Do something for some time

// get the end time
let end = Date.now();
// time elapsed
let elapsed = end - start;   // elapsed time in milliseconds

But it's not that simple as a Date object has a day, month, year, hour, minute and second values, hence we should take care while finding the elapsed time.

In this tutorial, we covered the Date object in JavaScript which is used in many web applications.



About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight