JavaScript Date Object Usage Summary _javascript tips

Source: Internet
Author: User
Tags getdate local time
Global functions
Date
Static methods for the Date class
Date.parse
Date.utc
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 methods for 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
--------------------------------------------------------------------------------
Establishes a time object based on one or more values, and the difference between local timing and world standard timing
--------------------------------------------------------------------------------
First, create a time object in the easiest way to understand it.
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, Mar 2009 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 time.
Alert (d.valueof ()); 1238129999999
Alert (D.gettime ()); 1238129999999
/* Get the local time and world standard timing of the jet lag * *
Alert (D.gettimezoneoffset ()); -480; The unit is minutes, which is 8 hours.
/* Direct use of the time value (milliseconds, such as: 1238129999999) to establish the time object * *
var d = new Date (1238129999999);
Alert (d.tolocalestring ()); March 27, 2009 12:59:59
* * But when the function has 2-7 parameters, it will be based on "year, month, day, time, minute, second, millisecond" Establishment 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/* Date class has a static function of UTC with the same argument as the above 2-7, but UTC gets a number * *
var n = DATE.UTC (2009, 0); This just gets the number of milliseconds to represent the time.
Alert (typeof N); Number
var d = new Date (n); Based on the number just obtained, re-establish as a time object
Alert (d.toutcstring ()); Thu, 1 2009 00:00:00 UTC
Alert (d.tolocalestring ()); January 1, 2009 8:00:00
--------------------------------------------------------------------------------
The difference between a time object that has no parameters, and the time that is obtained with the global function date
--------------------------------------------------------------------------------
var D1 = new Date (); Return Time Object
var d2 = Date (); Return time string
alert (D1); Fri Feb 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
Obviously D2 is just a string, it can use the method of the string class, but not the method of the Date class.
--------------------------------------------------------------------------------
To establish a time object using string parameters
--------------------------------------------------------------------------------
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 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 parse of the Date class also requires 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 day of the week is available but cannot be 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, 2 means 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, 1 means 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 1970 00:00:00 UTC
Related Article

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.