Javascript Date - 45 Methods (Reference)

Tech:
Javascript
Since:
1 year ago
Views:
8

The Javascript Date object is a built-in object that provides methods for performing date and time operations. It is used to represent a point in time, which can be either local or UTC.

getDay()

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

var date = new Date();
// Sat Oct 03 2020 14:57:47 GMT+0530 (India Standard Time)

var x = date.getDay();
// 6

// Sun - 0
// Mon - 1
// Tue - 2
// Wed - 3
// Thu - 4
// Fri - 5
// Sat - 6

getMonth()

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

var date = new Date();
// Sat Oct 03 2020 14:57:47 GMT+0530 (India Standard Time)

var x = date.getMonth();
// 96

// Jan - 0
// Feb - 1
// Mar - 2
// Apr - 3
// May - 4
// Jun - 5
// Jul - 6
// Aug - 7
// Sep - 8
// Oct - 9
// Nov - 10
// Dec - 11

getDate()

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

var date = new Date();
// Sat Oct 03 2020 14:57:47 GMT+0530 (India Standard Time)

var x = date.getDate();
// 3 (1 - 31)

getFullYear()

The getFullYear() method returns the year for the specified date according to local time.

var date = new Date();
// Sat Oct 03 2020 14:57:47 GMT+0530 (India Standard Time)

var x = date.getFullYear();
// 2020

getHours()

The getHours() method returns the hour for the specified date according to local time.

var date = new Date();
// Sat Oct 03 2020 14:57:47 GMT+0530 (India Standard Time)

var x = date.getHours();
// 14 (0 - 23)

getMinutes()

The getMinutes() method returns the minutes for the specified date according to local time.

var date = new Date();
// Sat Oct 03 2020 14:57:47 GMT+0530 (India Standard Time)

var x = date.getMinutes();
// 57 (0 - 59)

getSeconds()

The getSeconds() method returns the seconds for the specified date according to local time.

var date = new Date();
// Sat Oct 03 2020 14:57:47 GMT+0530 (India Standard Time)

var x = date.getSeconds();
// 47 (0 - 59)

getMilliseconds()

The getMilliseconds() method returns the milliseconds for the specified date according to local time.

var date = new Date();
// Sat Oct 03 2020 14:57:47 GMT+0530 (India Standard Time)

var x = date.getMilliseconds();
// 0 (0 - 999)

getUTCDay()

The getUTCDay() method returns the day of the week for the specified date according to universal time.

var date = new Date();
// Sat Oct 03 2020 14:57:47 GMT+0530 (India Standard Time)

var x = date.getUTCDay();
// 6

// Sun - 0
// Mon - 1
// Tue - 2
// Wed - 3
// Thu - 4
// Fri - 5
// Sat - 6

getUTCMonth()

The getUTCMonth() method returns the month for the specified date according to universal time.

var date = new Date();
// Sat Oct 03 2020 14:57:47 GMT+0530 (India Standard Time)

var x = date.getUTCMonth();
// 9

// Jan - 0
// Feb - 1
// Mar - 2
// Apr - 3
// May - 4
// Jun - 5
// Jul - 6
// Aug - 7
// Sep - 8
// Oct - 9
// Nov - 10
// Dec - 11

getUTCDate()

The getUTCDate() method returns the day of the month for the specified date according to universal time.

var date = new Date();
// Sat Oct 03 2020 14:57:47 GMT+0530 (India Standard Time)

var x = date.getUTCDate();
// 3 (1 - 31)

getUTCFullYear()

The getUTCFullYear() method returns the year for the specified date according to universal time.

var date = new Date();
// Sat Oct 03 2020 14:57:47 GMT+0530 (India Standard Time)

var x = date.getUTCFullYear();
// 2020

getUTCHours()

The getUTCHours() method returns the hour for the specified date according to universal time.

var date = new Date();
// Sat Oct 03 2020 14:57:47 GMT+0530 (India Standard Time)

var x = date.getUTCHours();
// 9 (0 - 23)

getUTCMinutes()

The getUTCMinutes() method returns the minutes for the specified date according to universal time.

var date = new Date();
// Sat Oct 03 2020 14:57:47 GMT+0530 (India Standard Time)

var x = date.getUTCMinutes();
// 42 (0 - 59)

getUTCSeconds()

The getUTCSeconds() method returns the seconds for the specified date according to universal time.

var date = new Date();
// Sat Oct 03 2020 14:57:47 GMT+0530 (India Standard Time)

var x = date.getUTCSeconds();
// 56 (0 - 56)

getUTCMilliseconds()

The getUTCMilliseconds() method returns the milliseconds for the specified date according to universal time.

var date = new Date();
// Sat Oct 03 2020 14:57:47 GMT+0530 (India Standard Time)

var x = date.getUTCMilliseconds();
// 48 (0 - 999)

getTime()

The getTime() method returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.

var date = new Date();
// Sat Oct 03 2020 14:57:47 GMT+0530 (India Standard Time)

var x = date.getTime();
// 1601718244579
// ms since January 1, 1970

setTime()

The setTime() method sets the date and time for the specified date according to universal time.

var date = new Date();
// Sat Oct 03 2020 14:57:47 GMT+0530 (India Standard Time)

var x = date.setTime(1332403882588);
// Thu Mar 22 2012 13:41:22 GMT+0530 (India Standard Time)
// add ms to January 1, 1970

getTimezoneOffset()

The getTimezoneOffset() method returns the time-zone offset from UTC, in minutes, for the current locale.

var date = new Date();
// Sat Oct 03 2020 14:57:47 GMT+0530 (India Standard Time)

var x = date.getTimezoneOffset();
// -330
// Diff - UTC time and local time, in min

now()

The now() method returns the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC.

var x = Date.now();
// 1601718553895
// ms since January 1, 1970

parse()

The parse() method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.

var x = Date.parse("March 21, 2012");
// 1332268200000
// ms since January 1, 1970 to March 21, 2012

UTC()

The UTC() method converts the specified date to UTC.

var x = Date.UTC(2012, 02, 30);
// 1333065600000
// ms since January 1, 1970 to 2012, 02, 30

setMonth()

The setMonth() method sets the month for a specified date according to local time.

var date = new Date();
// Sat Oct 03 2020 14:57:47 GMT+0530 (India Standard Time)

date.setMonth(4);
// Sun May 03 2020 15:27:03 GMT+0530 (India Standard Time)

setDate()

The setDate() method sets the day of the month for a specified date according to local time.

var date = new Date();
// Sat Oct 03 2020 14:57:47 GMT+0530 (India Standard Time)

date.setDate(15);
// Thu Oct 15 2020 15:27:22 GMT+0530 (India Standard Time)

setFullYear()

The setFullYear() method sets the year for a specified date according to local time.

var date = new Date();
// Sat Oct 03 2020 14:57:47 GMT+0530 (India Standard Time)

date.setFullYear(2020);
// Sat Oct 03 2020 15:27:38 GMT+0530 (India Standard Time)

setHours()

The setHours() method sets the hour for a specified date according to local time.

var date = new Date();
// Sat Oct 03 2020 15:31:42 GMT+0530 (India Standard Time)

date.setHours(16);
// Sat Oct 03 2020 16:31:42 GMT+0530 (India Standard Time)

setMinutes()

The setMinutes() method sets the minutes for a specified date according to local time.

var date = new Date();
// Sat Oct 03 2020 15:32:17 GMT+0530 (India Standard Time)

date.setMinutes(40);
// Sat Oct 03 2020 15:40:17 GMT+0530 (India Standard Time)

setSeconds()

The setSeconds() method sets the seconds for a specified date according to local time.

var date = new Date();
// Sat Oct 03 2020 15:33:02 GMT+0530 (India Standard Time)

date.setSeconds(20);
// Sat Oct 03 2020 15:33:20 GMT+0530 (India Standard Time)

setMilliseconds()

The setMilliseconds() method sets the milliseconds for a specified date according to local time.

var date = new Date();
// Sat Oct 03 2020 14:57:47 GMT+0530 (India Standard Time)

date.setMilliseconds(100);

setUTCMonth()

The setUTCMonth() method sets the month for a specified date according to universal time.

var date = new Date();
// Sat Oct 03 2020 14:57:47 GMT+0530 (India Standard Time)

date.setUTCMonth(4);
// Sun May 03 2020 15:37:00 GMT+0530 (India Standard Time)

setUTCDate()

The setUTCDate() method sets the day of the month for a specified date according to universal time.

var date = new Date();
// Sat Oct 03 2020 14:57:47 GMT+0530 (India Standard Time)

date.setUTCDate(15);
// Thu Oct 15 2020 15:37:27 GMT+0530 (India Standard Time)

setUTCFullYear()

The setUTCFullYear() method sets the year for a specified date according to universal time.

var date = new Date();
// Sat Oct 03 2020 14:57:47 GMT+0530 (India Standard Time)

date.setUTCFullYear(2020);
// Sat Oct 03 2020 15:38:46 GMT+0530 (India Standard Time)

setUTCHours()

The setUTCHours() method sets the hour for a specified date according to universal time.

var date = new Date();
// Sat Oct 03 2020 15:40:34 GMT+0530 (India Standard Time)

date.setUTCHours(15);
// Sat Oct 03 2020 20:40:34 GMT+0530 (India Standard Time)

setUTCMinutes()

The setUTCMinutes() method sets the minutes for a specified date according to universal time.

var date = new Date();
// Sat Oct 03 2020 15:41:19 GMT+0530 (India Standard Time)

date.setUTCMinutes(17);
// Sat Oct 03 2020 15:47:19 GMT+0530 (India Standard Time)

setUTCSeconds()

The setUTCSeconds() method sets the seconds for a specified date according to universal time.

var date = new Date();
// Sat Oct 03 2020 15:42:18 GMT+0530 (India Standard Time)

date.setUTCSeconds(35);
// Sat Oct 03 2020 15:42:35 GMT+0530 (India Standard Time)

setUTCMilliseconds()

The setUTCMilliseconds() method sets the milliseconds for a specified date according to universal time.

var date = new Date();
// Sat Oct 03 2020 14:57:47 GMT+0530 (India Standard Time)

date.setUTCMilliseconds(100);

toString()

The toString() method returns a string representation of the date.

var date = new Date();
// Sat Oct 03 2020 14:57:47 GMT+0530 (India Standard Time)

var x = date.toString();
// Sat Oct 03 2020 15:50:16 GMT+0530 (India Standard Time)

toLocaleString()

The toLocaleString() method returns a string representation of the date, based on the locale and the options.

var date = new Date();
// Sat Oct 03 2020 14:57:47 GMT+0530 (India Standard Time)

var x = date.toLocaleString();
// 03/10/2020, 15:50:39

toISOString()

The toISOString() method returns a string representation of the date, based on the ISO 8601 format.

var date = new Date();
// Sat Oct 03 2020 14:57:47 GMT+0530 (India Standard Time)

var x = date.toISOString();
// 2020-10-03T10:20:45.882Z

toJSON()

The toJSON() method returns a string representation of the date, based on the ISO 8601 format.

var date = new Date();
// Sat Oct 03 2020 14:57:47 GMT+0530 (India Standard Time)

var x = date.toJSON();
// 2020-10-03T10:20:52.042Z

valueOf()

The valueOf() method returns the primitive value of the specified object.

var date = new Date();
// Sat Oct 03 2020 14:57:47 GMT+0530 (India Standard Time)

var x = date.valueOf();
// 1601720459168

toTimeString()

The toTimeString() method returns a string representation of the date, based on the time.

var date = new Date();
// Sat Oct 03 2020 15:52:09 GMT+0530 (India Standard Time)

var x = date.toTimeString();
// 15:52:09 GMT+0530 (India Standard Time)

toDateString()

The toDateString() method returns a string representation of the date, based on the date.

var date = new Date();
// Sat Oct 03 2020 15:52:49 GMT+0530 (India Standard Time)

var x = date.toDateString();
// Sat Oct 03 2020

toLocaleTimeString()

The toLocaleTimeString() method returns a string representation of the date, based on the locale and the options.

var date = new Date();
// Sat Oct 03 2020 15:53:15 GMT+0530 (India Standard Time)

var x = date.toLocaleTimeString();
// 15:53:15

toLocaleDateString()

The toLocaleDateString() method returns a string representation of the date, based on the locale and the options.

var date = new Date();
// Sat Oct 03 2020 15:53:36 GMT+0530 (India Standard Time)

var x = date.toLocaleDateString();
// 03/10/2020