Summary of date usage in JavaScript (goto)

Source: Internet
Author: User
Tags goto

//Global function
Date

static methods of the//date class
Date.parse
Date.utc

the method of establishing//date object
New Date ()
New Date (number of milliseconds)
New Date (Standard Time format string)
New Date (year, month, day, time, minute, second, millisecond)

more ways to//date objects
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
--------------------------------------------------------------------------------

Establish time objects based on one or more values, and the difference between local timings and world standard timekeeping
--------------------------------------------------------------------------------

Set up a Time object in the easiest way to understand it first
var d = new Date (2009, 2, 27, 12, 59, 59, 999);

alert (d); Fri Mar 12:59:59 utc+0800 2009
Alert (d.tostring ()); Fri Mar 12:59:59 utc+0800 2009
Alert (d.toutcstring ()); Fri, 04:59:59 UTC
Alert (d.tolocalestring ()); March 27, 2009 12:59:59

Alert (d.todatestring ()); Fri Mar 27 2009
Alert (d.tolocaledatestring ()); March 27, 2009

Alert (d.totimestring ()); 12:59:59 utc+0800
Alert (d.tolocaletimestring ()); 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 to this time from UTC */
Alert (d.valueof ()); 1238129999999
Alert (D.gettime ()); 1238129999999

/* Get the time difference between local and world standard timings */
Alert (D.gettimezoneoffset ()); -480; Units are minutes, that's 8 hours.

/* Directly use the time value (in milliseconds, e.g. above: 1238129999999) to set up the time object */
var d = new Date (1238129999999);
Alert (d.tolocalestring ()); March 27, 2009 12:59:59

/* But when a function has 2-7 parameters, it will be based on "year, month, day, hour, minute, second, millisecond" settling time */
D = new Date (2009, 2, 27, 12, 59, 59, 999);
Alert (d.tolocalestring ()); March 27, 2009 12:59:59

D = new Date (2009, 2, 27, 12, 59, 59);
Alert (d.tolocalestring ()); March 27, 2009 12:59:59

D = new Date (2009, 2, 27, 12, 59);
Alert (d.tolocalestring ()); March 27, 2009 12:59:00

D = new Date (2009, 2, 27, 12);
Alert (d.tolocalestring ()); March 27, 2009 12:00:00

D = new Date (2009, 2, 27);
Alert (d.tolocalestring ()); March 27, 2009 0:00:00

D = new Date (2009, 2);
Alert (d.tolocalestring ()); March 1, 2009 0:00:00

/* The static function of the Date class UTC has the same 2-7 parameters as above, but UTC gets a number */
var n = DATE.UTC (2009, 0); It just gets the number of milliseconds that represents the time.
Alert (typeof N); Number

var d = new Date (n); Re-establish as a time object based on the number you just acquired
Alert (d.toutcstring ()); Thu, 1 Jan 00:00:00 UTC
Alert (d.tolocalestring ()); January 1, 2009 8:00:00
--------------------------------------------------------------------------------


The difference between the time object created by the parameterless and the time obtained with the global function date
--------------------------------------------------------------------------------

var D1 = new Date (); Return Time Object
var d2 = Date (); Return time string

alert (D1); Fri 13:20:58 utc+0800 2009
Alert (D2); Fri Feb 27 13:20:58 2009

Alert (d1.valueof ()); 1235712058340
Alert (d2.valueof ()); 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 method of the string class, but not the method of the Date class.
--------------------------------------------------------------------------------


To set up a time object with a string argument
--------------------------------------------------------------------------------

var D;
D = new Date (' Fri Mar 12:59:59 utc+0800 2009 ');
Alert (d.tolocalestring ()); March 27, 2009 12:59:59

D = new Date (' Fri Mar 27 12:59:59 2009 ');
Alert (d.tolocalestring ()); March 27, 2009 12:59:59

D = new Date (' Fri Mar 27 2009 ');
Alert (d.tolocalestring ()); March 27, 2009 0:00:00

D = new Date (' Mar 27 2009 ');
Alert (d.tolocalestring ()); March 27, 2009 0:00:00

/* You can use the string returned by Date () */
var ts = Date ();
D = new Date (TS);
Alert (d.tolocalestring ()); March 27, 2009 14:04:38

/* The static function of the Date class, Parse, is also required for the same character parameter, but it returns a number (that number of milliseconds) */
var n;
n = Date.parse (' Mar 27 2009 ');
alert (n); 1238083200000
Alert (typeof N); Number

D = new Date (n);
Alert (d.tolocalestring ()); March 27, 2009 0:00:00
--------------------------------------------------------------------------------


Get and set separately: year, month, day, time, minute, second, millisecond, where "days of the Week" can be obtained but not set
--------------------------------------------------------------------------------

var d = new Date (2009, 2, 27, 12, 58, 59, 999);
Alert (d.tolocalestring ()); March 27, 2009 0:00:00
Alert (D.getfullyear ()); 2009
Alert (D.getmonth ()); 2 (from 0 onwards, 2 refers to March)
Alert (D.getdate ()); 27
Alert (D.getday ()); 5 (Friday)
Alert (d.gethours ()); 12
Alert (D.getminutes ()); 58
Alert (D.getseconds ()); 59
Alert (D.getmilliseconds ()); 999 (MS)

D.setfullyear (2010);
D.setmonth (1);
D.setdate (2);
D.sethours (3);
D.setminutes (4);
D.setseconds (5);
D.setmilliseconds (6);

Alert (d.tolocalestring ()); February 2, 2010 3:04:05
Alert (D.getfullyear ()); 2010
Alert (D.getmonth ()); 1 (from 0 onwards, 1 refers to February)
Alert (D.getdate ()); 2
Alert (D.getday ()); 2 (Tuesday)
Alert (d.gethours ()); 3
Alert (D.getminutes ()); 4
Alert (D.getseconds ()); 5
Alert (D.getmilliseconds ()); 6 (MS)

/* There is also a settime * *
var d = new Date ();
D.settime (0);
Alert (d.toutcstring ()); Thu, 1 Jan 1970 00:00:00 UTC

Summary of date usage in JavaScript (goto)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.