JS Date Object

JavaScript

In this tutorial we will learn about JavaScript Date Object.

We use the Date object to set and get certain time value.

Instance of the Date object

To create an instance of the Date object we use the new keyword.

var dateObj = new Date();

Date syntax

Following are the syntax for creating instance of the Date object.

new Date();
new Date(value);
new Date(dateString);
new Date(year, month[, date[, hours[, minutes[, seconds[, milliseconds]]]]]);

value is the integer value representing time in milliseconds since 1st January 1970, 00:00:00 UTC.

dateString is a string representing date in a format that is valid for the Date.parse() method.

year is integer value for year. 0 to 99 map to the years 1900 to 1999. For others use full year i.e., 2017.

month is integer value for month. 0 for January and 11 for December.

date (optional) is integer value for day of the month. Value range 1-31.

hours (optional) is integer value for hour. Value range 0-23.

minutes (optional) is integer value for minutes. Value range 0-59.

seconds (optional) is integer value for seconds. Value range 0-59.

milliseconds (optional) is integer value for milliseconds. Value range 0-999.

Current local time

The instance of the the Date object holds the date time of the moment when it was created. In the following example we are printing the date time in the browser console.

var dateObj = new Date();
console.log(dateObj);	//sample output: Mon Jan 27 2014 20:04:43 GMT+0530 (IST)

Following are some of the methods of the Date object.

Getter Methods of Date object

The following methods are used to get date value.

getDate( )

We use the getDate() method to get the day of the month (1-31) for the specified date based on local time.

var dateObj = new Date();
console.log(dateObj.getDate());		//sample output: 25

getDay( )

We use the getDay() method to get the day of the week (0-6) for the specified date based on local time.

0 is for Sunday, 1 is for Monday and so on.

var dateObj = new Date();
console.log(dateObj.getDay());		//sample output: 6

getFullYear( )

We use the getFullYear() method to get the full year (4 digit) for the speicified date based on local time.

var dateObj = new Date();
console.log(dateObj.getFullYear());		//sample output: 2017

getHours( )

We use the getHours() method to get the hour (0-23) for the speicified date based on local time.

var dateObj = new Date();
console.log(dateObj.getHours());		//sample output: 22

getMilliseconds( )

We use the getMilliseconds() method to get the milliseconds (0-999) for the speicified date based on local time.

var dateObj = new Date();
console.log(dateObj.getMilliseconds());		//sample output: 558

getMinutes( )

We use the getMinutes() method to get the minutes (0-59) for the speicified date based on local time.

var dateObj = new Date();
console.log(dateObj.getMinutes());		//sample output: 42

getMonth( )

We use the getMonth() method to get the month (0-11) for the speicified date based on local time.

0 is for January, 1 is for February and so on.

var dateObj = new Date();
console.log(dateObj.getMonth());		//sample output: 1

getSeconds( )

We use the getSeconds() method to get the seconds (0-59) for the speicified date based on local time.

var dateObj = new Date();
console.log(dateObj.getSeconds());		//sample output: 24

getTime( )

We use the getTime() method to get the numeric value of the specified date in milliseconds since 1st January 1970, 00:00:00 UTC.

var dateObj = new Date();
console.log(dateObj.getTime());		//sample output: 1415911137305

getTimezoneOffset( )

We use the getTimezoneOffset() method to get timezone offset in minutes for the given local time.

var dateObj = new Date();
console.log(dateObj.getTimezoneOffset());		//sample output: -330

getUTCDate( )

We use the getUTCDate() method to get the day of the month (1-31) according to Universal time.

var dateObj = new Date();
console.log(dateObj.getUTCDate());		//sample output: 25

getUTCDay( )

We use the getUTCDay() method to get the day of the week (0-6) according to Universal time.

0 is for Sunday, 1 is for Monday and so on.

var dateObj = new Date();
console.log(dateObj.getUTCDay());		//sample output: 6

getUTCFullYear( )

We use the getUTCFullYear() method to get the full year (4 digit) according to Universal time.

var dateObj = new Date();
console.log(dateObj.getUTCFullYear());		//sample output: 2017

getUTCHours( )

We use the getUTCHours() method to get the hour (0-23) according to Universal time.

var dateObj = new Date();
console.log(dateObj.getUTCHours());		//sample output: 17

getUTCMilliseconds( )

We use the getUTCMilliseconds() method to get the milliseconds (0-999) according to Universal time.

var dateObj = new Date();
console.log(dateObj.getUTCMilliseconds());		//sample output: 558

getUTCMinutes( )

We use the getUTCMinutes() method to get the minutes (0-59) according to Universal time.

var dateObj = new Date();
console.log(dateObj.getUTCMinutes());		//sample output: 42

getUTCMonth( )

We use the getUTCMonth() method to get the month (0-11) according to specified time.

0 is for January, 1 is for February and so on.

var dateObj = new Date();
console.log(dateObj.getUTCMonth());		//sample output: 1

getUTCSeconds( )

We use the getUTCSeconds() method to get the seconds (0-59) according to Universal time.

var dateObj = new Date();
console.log(dateObj.getUTCSeconds());		//sample output: 9

Setter Methods of Date object

The following methods are used to set date value.

setDate( )

We use the setDate() method to set the day of the month (1-31) of the Date object relative to the previously set date.

var dateObj = new Date(2014, 0, 1);	//date: 1st Jan 2014

console.log(dateObj);		//sample output: Wed Jan 01 2014 00:00:00 GMT+0530 (IST)

dateObj.setDate(5);

console.log(dateObj);		//sample output: Sun Jan 05 2014 00:00:00 GMT+0530 (IST)

setFullYear( )

We use the setFullYear() method to set the year (like 2015) of the Date object relative to the previously set date.

var dateObj = new Date(2014, 0, 1);	//date: 1st Jan 2014

console.log(dateObj);		//sample output: Wed Jan 01 2014 00:00:00 GMT+0530 (IST)

dateObj.setFullYear(2015);

console.log(dateObj);		//sample output: Thu Jan 01 2015 00:00:00 GMT+0530 (IST)

setHours( )

We use the setHours() method to set the hour of the day (0-23) of the Date object relative to the previously set date.

var dateObj = new Date(2014, 0, 1, 10, 20, 30);	//date: 1st Jan 2014 10:20:30

console.log(dateObj);		//sample output: Wed Jan 01 2014 10:20:30 GMT+0530 (IST)

dateObj.setHours(15);

console.log(dateObj);		//sample output: Wed Jan 01 2014 15:20:30 GMT+0530 (IST)

setMilliseconds( )

We use the setMilliseconds() method to set the milliseconds of the day (0-999) of the Date object relative to the previously set date.

var dateObj = new Date(2014, 0, 1, 10, 20, 30, 500);	//date: 1st Jan 2014 10:20:30 500

console.log(dateObj.getMilliseconds());		//sample output: 500

dateObj.setMilliseconds(700);

console.log(dateObj.getMilliseconds());		//sample output: 700

setMinutes( )

We use the setMinutes() method to set the minutes (0-59) of the Date object relative to the previously set date.

var dateObj = new Date(2014, 0, 1, 10, 20, 30);	//date: 1st Jan 2014 10:20:30

console.log(dateObj);		//sample output: Wed Jan 01 2014 10:20:30 GMT+0530 (IST)

dateObj.setMinutes(50);

console.log(dateObj);		//sample output: Wed Jan 01 2014 10:50:30 GMT+0530 (IST)

setMonth( )

We use the setMonth() method to set the month (0-11) of the Date object relative to the previously set date.

0 for January and 11 for December.

var dateObj = new Date(2014, 0, 1, 10, 20, 30);	//date: 1st Jan 2014 10:20:30

console.log(dateObj);		//sample output: Wed Jan 01 2014 10:20:30 GMT+0530 (IST)

dateObj.setMonth(5);

console.log(dateObj);		//sample output: Sun Jun 01 2014 10:20:30 GMT+0530 (IST)

setSeconds( )

We use the setSeconds() method to set the seconds (0-59) of the Date object relative to the previously set date.

var dateObj = new Date(2014, 0, 1, 10, 20, 30);	//date: 1st Jan 2014 10:20:30

console.log(dateObj);		//sample output: Wed Jan 01 2014 10:20:30 GMT+0530 (IST)

dateObj.setSeconds(50);

console.log(dateObj);		//sample output: Wed Jan 01 2014 10:20:50 GMT+0530 (IST)

setTime( )

We use the setTime() method to set the time in milliseconds since 1st January 1970 00:00:00 UTC.

var dateObj = new Date(2014, 0, 1, 10, 20, 30);	//date: 1st Jan 2014 10:20:30

console.log(dateObj);		//sample output: Wed Jan 01 2014 10:20:30 GMT+0530 (IST)

dateObj.setTime(1456789012345);

console.log(dateObj);		//sample output: Tue Mar 01 2016 05:06:52 GMT+0530 (IST)

setUTCDate( )

We use the setUTCDate() method to set the day of the month (1-31) of specified date according to Universal time.

var dateObj = new Date();

console.log(dateObj);		//sample output: Tue Feb 26 2013 21:58:11 GMT+0530 (IST)

dateObj.setUTCDate(5);

console.log(dateObj);		//sample output: Tue Feb 05 2013 21:58:11 GMT+0530 (IST)

setUTCFullYear( )

We use the setUTCFullYear() method to set the year (like 2015) of specified date according to Universal time.

var dateObj = new Date();

console.log(dateObj);		//sample output: Tue Feb 26 2013 21:59:45 GMT+0530 (IST)

dateObj.setUTCFullYear(2015);

console.log(dateObj);		//sample output: Thu Feb 26 2015 22:01:48 GMT+0530 (IST)

setUTCHours( )

We use the setUTCHours() method to set the hour of the day (0-23) of specified date according to Universal time.

var dateObj = new Date();

console.log(dateObj);		//sample output: Tue Feb 26 2013 22:13:51 GMT+0530 (IST)

dateObj.setUTCHours(15);

console.log(dateObj);		//sample output: Tue Feb 26 2013 21:13:51 GMT+0530 (IST)

setUTCMilliseconds( )

We use the setUTCMilliseconds() method to set the milliseconds (0-999) of specified date according to Universal time.

var dateObj = new Date();

console.log(dateObj.getUTCMilliseconds());		//sample output: 263

dateObj.setUTCMilliseconds(700);

console.log(dateObj.getUTCMilliseconds());		//sample output: 700

setUTCMinutes( )

We use the setUTCMinutes() method to set the minutes (0-59) of specified date according to Universal time.

var dateObj = new Date();

console.log(dateObj);		//sample output: Thu Feb 27 2014 09:52:22 GMT+0530 (IST)

dateObj.setUTCMinutes(50);

console.log(dateObj);		//sample output: Thu Feb 27 2014 10:20:22 GMT+0530 (IST)

setUTCMonth( )

We use the setUTCMonth() method to set the month (0-11) of specified date according to Universal time.

0 for January and 11 for December.

var dateObj = new Date();

console.log(dateObj);		//sample output: Thu Feb 27 2014 09:55:55 GMT+0530 (IST)

dateObj.setUTCMonth(5);

console.log(dateObj);		//sample output: Fri Jun 27 2014 09:55:55 GMT+0530 (IST)

setUTCSeconds( )

We use the setUTCSeconds() method to set the seconds (0-59) of specified date according to Universal time.

var dateObj = new Date();

console.log(dateObj);		//sample output: Thu Feb 27 2014 09:58:04 GMT+0530 (IST)

dateObj.setUTCSeconds(50);

console.log(dateObj);		//sample output: Thu Feb 27 2014 09:58:50 GMT+0530 (IST)

Some more Date Object methods

toDateString( )

The toDateString() method returns the date portion of the Date object in human readable form in American English.

var dateObj = new Date();

var str = dateObj.toDateString();

console.log(str);	//sample output: Thu Feb 27 2014

toString( )

The toString() method returns the Date object in string in human readable form in American English.

var dateObj = new Date();

var str = dateObj.toString();

console.log(str);	//sample output: Thu Feb 27 2014 10:15:02 GMT+0530 (IST)

toUTCString( )

The toUTCString() method returns the Date object in string in Universal time zone.

var dateObj = new Date();

var str = dateObj.toUTCString();

console.log(str);	//sample output: Thu, Feb 27 2014 04:46:54 GMT