SoFunction
Updated on 2025-02-28

JavaScript Date object usage summary

//Global Function
Date
// Static method of Date class


//Date object creation method
new Date()
new Date (milliseconds)
new Date (standard time format string)
new Date(year, month, day, hour, minute, second, millisecond)
//More methods of Date object
getFullYear (getUTCFullYear)
getMonth (getUTCMonth)
getDate (getUTCDate)
getDay (getUTCDay)
getHours (getUTCHours)
getMinutes (getUTCMinutes)
getSeconds (getUTCSeconds)
getMilliseconds (getUTCMilliseconds)
getTime
getTimezoneOffset
setFullYear (setUTCFullYear)
setMonth (setUTCMonth)
setDate (setUTCDate)
setHours (setUTCHours)
setMinutes (setUTCMinutes)
setSeconds (setUTCSeconds)
setMilliseconds (setUTCMilliseconds)
setTime
toDateString
toTimeString
toUTCString
toLocaleString
toLocaleDateString
toLocaleTimeString
toString
valueOf
--------------------------------------------------------------------------------
Create time objects based on one or more numerical values, and the difference between local timing and world standard timing
--------------------------------------------------------------------------------
// Create a time object in the easiest way to understand first
var d = new Date(2009, 2, 27, 12, 59, 59, 999);
alert(d); //Fri Mar 27 12:59:59 UTC+0800 2009
alert(()); //Fri Mar 27 12:59:59 UTC+0800 2009
alert(()); //Fri, 27 Mar 2009 04:59:59 UTC
alert(()); // March 27, 2009 12:59:59
alert(()); //Fri Mar 27 2009
alert(()); // March 27, 2009
alert(()); //12:59:59 UTC+0800
alert(()); //12:59:59
/* Time is recorded as an integer in the computer, which is the number of milliseconds from 1970-1-1 0:0:0 of ​​UTC time to this time */
alert(()); //1238129999999
alert(()); //1238129999999
/* Get the time difference between local time and world standard time */
alert(()); //-480; Unit is minutes, that is, 8 hours
/* Use the time value directly (milliseconds, such as above: 12381299999999) to create a time object */
var d = new Date(1238129999999);
alert(()); // March 27, 2009 12:59:59
/* But when the build function has 2-7 parameters, it will be based on the "year, month, day, hour, minute, seconds, millisecond" setting time */
d = new Date(2009, 2, 27, 12, 59, 59, 999);
alert(()); // March 27, 2009 12:59:59
d = new Date(2009, 2, 27, 12, 59, 59);
alert(()); // March 27, 2009 12:59:59
d = new Date(2009, 2, 27, 12, 59);
alert(()); // March 27, 2009 12:59:00
d = new Date(2009, 2, 27, 12);
alert(()); // March 27, 2009 12:00:00
d = new Date(2009, 2, 27);
alert(()); // March 27, 2009 0:00:00
d = new Date(2009, 2);
alert(()); // March 1, 2009 0:00:00
/* The static function of the Date class UTC parameter is also 2-7 as above, but UTC gets a number */
var n = (2009, 0); //This is just getting the number of milliseconds representing time
alert(typeof n); //number
var d = new Date(n); //Re-create as a time object based on the number just obtained
alert(()); //Thu, 1 Jan 2009 00:00:00 UTC
alert(()); // January 1, 2009 8:00:00
--------------------------------------------------------------------------------
The difference between a time object created without parameters and a time obtained with a global function Date
--------------------------------------------------------------------------------
var d1 = new Date(); //Return the time object
var d2 = Date(); //Return the time string
alert(d1); //Fri Feb 27 13:20:58 UTC+0800 2009
alert(d2); //Fri Feb 27 13:20:58 2009
alert(()); //1235712058340
alert(()); //Fri Feb 27 13:20:58 2009
alert(typeof d1); //object
alert(typeof d2); //string
//It is obvious that d2 is just a string. It can use the methods of the String class, but not the methods of the Date class.
--------------------------------------------------------------------------------
Create a time object using string parameters
--------------------------------------------------------------------------------
var d;
d = new Date('Fri Mar 27 12:59:59 UTC+0800 2009');
alert(()); // March 27, 2009 12:59:59
d = new Date('Fri Mar 27 12:59:59 2009');
alert(()); // March 27, 2009 12:59:59
d = new Date('Fri Mar 27 2009');
alert(()); // March 27, 2009 0:00:00
d = new Date('Mar 27 2009');
alert(()); // March 27, 2009 0:00:00
/* The string returned by Date() can be used */
var ts = Date();
d = new Date(ts);
alert(()); // March 27, 2009 14:04:38
/* The static function parse of the Date class also requires the same character parameter, but it returns a number (that millisecond number) */
var n;
n = ('Mar 27 2009');
alert(n); //1238083200000
alert(typeof n); //number
d = new Date(n);
alert(()); // March 27, 2009 0:00:00
--------------------------------------------------------------------------------
Get and set separately: year, month, day, hour, minute, seconds, milliseconds, among which "day of the week" can be obtained but cannot be set
--------------------------------------------------------------------------------
var d = new Date(2009, 2, 27, 12, 58, 59, 999);
alert(()); // March 27, 2009 0:00:00
alert(()); //2009
alert(()); //2 (start from 0, 2 means 3 months)
alert(()); //27
alert(()); //5 (Friday)
alert(()); //12
alert(()); //58
alert(()); //59
alert(()); //999 (ms)
(2010);
(1);
(2);
(3);
(4);
(5);
(6);
alert(()); //February 2, 2010 3:04:05
alert(()); //2010
alert(()); //1 (From 0, 1 means February)
alert(()); //2
alert(()); //2 (Tuesday)
alert(()); //3
alert(()); //4
alert(()); //5
alert(()); //6 (ms)
/* There is also a setTime */
var d = new Date();
(0);
alert(()); //Thu, 1 Jan 1970 00:00:00 UTC