Tutorials Tonight's Logo
Follow Us


Javascript date is fundamentally the number of milliseconds that have elapsed since midnight on January 1, 1970, UTC.

Javascript provides Date object to fetch date and time. The Date object stores the current date, time and multiple methods to manage date and time.

Javascript Date object represents a single moment in time. Time value in the core of Date object is UTC, the basic method to fetch date and time all works on the local time zone.

Note: By default, javascript will use the browser's time Zone

year: ?
month: ?
day: ?
hour: ?
minute: ?
second: ?

How to Create Date object in Javascript

The Date object in javascript is created using Date() constructor. Look at the example below.

var today = new Date();
▶ Run the code

There are 4 different ways to create Date object in javascript.

  1. new Date()
  2. new Date(milliseconds)
  3. new Date(dateString)
  4. new Date(year, month, day, hours, minutes, seconds, milliseconds)

1. new date javascript

The new Date() return current date and time.

var today = new Date();
▶ Run the code

2. new Date milliseconds javascript

The Second method of creating a date object is new Date(milliseconds). This method return date and time passed after given milliseconds from midnight 1 January 1970 UTC.

var date1 = new Date(0);
var date2 = new Date(1000000); // 01 january, 1970 + 1000000milliseconds
▶ Run the code

3. new Date(dateString) Javascript

The new Date(dateString) accepts a String parameter named dateString. The String represents a date and new Date(dateString) return a standard date using the String.

var date1 = new Date("05 05, 1996 17:00:00");
var date2 = new Date("November 11, 1997 01:00:00");
▶ Run the code


4. new Date(year, month, day, hours, minutes, seconds, milliseconds)

The new Date(year, month, day, hours, minutes, seconds, milliseconds) accepts 7 parameters. This return standard date based on passed arguments. Lets look at all the 7 parameters:

  • year: It is interger value that represent year. Value passed for year should be full. example 2012 not 12. 2 digit year will be represented as 19xx.
  • month: It is integer value from 0 to 11 representing January(0) to December(11).
  • day: It is an integer value representing a date.
  • hours: It is an integer which represents hour on a 24-hour scale.
  • minutes: It is an integer value that represents the minute.
  • second: It is an integer value that represents second.
  • milliseconds: It is an integer value that represents milliseconds.

Among these 7 parameters, last 5 are optional but it is necessary to provide year and month.

var date1 = new Date(2010, 6, 21, 9, 15, 55, 250);
var date2 = new Date(2010, 6, 21, 9, 15, 55);
var date3 = new Date(2010, 6, 21, 9, 15);
var date4 = new Date(2010, 6, 21, 9);
var date5 = new Date(2010, 6, 21);
var date6 = new Date(2010, 6);
▶ Run the code

Javascript date methods

The Date object in javascript provides various methods to get specific information from the Date object.

Sr no. Method Description
1 getFullYear() Return current year as 4 digit number (yyyy)
2 getMonth() Return current month in number from 0 to 11
3 getDate() Return current date in number (1-31)
4 getHours() Return current hour in 24hour clock (0-23)
5 getMinutes() Return current minute in number (0-59)
6 getSeconds() Return current second in number (0-59)
7 getMilliseconds() Return current millisecond in number (0-999)

Let's see an example for each method.

1. getFullYear() Method

getFullYear() returns full year of current date.

Use getFullYear() not getYear() which return current year in 2 digit and is deprecated.

var year = new Date().getFullYear(); // current year
▶ Run the code

2. getMonth() Method

getMonth() method returns current month of the date in number from 0 to 11. 0 represent 'January' and 11 represent 'December'.

var month = new Date().getMonth() + 1; // +1 because 0 represent january
▶ Run the code

3. getDate() Method

getDate() method returns current date of the day in number from 1 to 11.

var date = new Date().getDate();
▶ Run the code

4. getHours() Method

getHours() method returns current hour in form of 24 hour clock from 0-23.

var hour = new Date().getHours();
▶ Run the code

5. getMinutes() Method

getMinutes() method returns current second in number from 0-59.

var minute = new Date().getMinutes();
▶ Run the code

6. getSeconds() Method

getSeconds() method returns current second in number from 0-59.

var second = new Date().getSeconds();
▶ Run the code

7. getMilliseconds() Method

getMilliseconds() method returns current millisecond in number from 0-999.

var milliseconds = new Date().getMilliseconds();
▶ Run the code

More Learning

Want to learn more about programming and web development? We have a few recommendations for you.

Follow Us

Copyright © Tutorials Tonight. All Rights Are Reserved.