Home »
JavaScript Examples
Ways to create Date object in JavaScript
In this article we will learn about various ways to create Date object in JavaScript.
Submitted by Abhishek Pathak, on October 19, 2017
Date object in JavaScript
Date object in JavaScript is used quite often. Whether it is countdown timer for an event or picking up a date through user Input, Dates in JavaScript have a lot of use cases and in JavaScript we have got lot of ways to create Date object.
Creating date with Date constructor
Creating date with Date constructor is the simplest way to create a date. Consider this example,
Example
var today = new Date();
The Date() constructor creates a new date object and stores it inside the today variable and the value of this variable is, "Wed Oct 18 2017 00:45:58 GMT+0530 (India Standard Time)", ie the date-time when this object was created.
Define a specific date
We can also define a specific date in the above format using this code,
Example
var event = new Date('December 25, 2017 00:00:00');
This will create an event variable with the specified Date value given as a string. Also note, instead of providing month in words, we can provide the date-time format.
Define a date object in specific format
The date time format is in following format, "YYYY-MM-DDTHH:MM:SS". Consider this example
Example
var event = new Date('2017-12-25T00:00:00');
Define a date object without specifying all fields
While providing time string is preferred in this format, but you can also specify date without giving all fields. This is also valid in JavaScript and works perfectly fine.
Example
var event = new Date(2017, 12, 25);
This will create an event object with only these values and other date parts as 00:00:00 by default.
These were the various methods to create date object in JavaScript. Share your thoughts in the comments below.
JavaScript Examples »