JavaScript Date Common point of knowledge

Source: Internet
Author: User
Tags local time
1. New Date (DATESTR): Converts a string to a Date objectParameters:
①datestr {string}: A string that can be converted to a Date object (can be omitted); There are two main types of strings:
1) yyyy/mm/dd HH:mm:ss: If the time is omitted, the time of the date object returned is 00:00:00.
2) Yyyy-mm-dd HH:mm:ss: If the time is omitted, the Date object returned is 08:00:00 (plus the local time zone). If you do not omit the time, this string will fail in IE conversion!
return value:

{Date} returns a converted Date object.

Example:

var dt = new Date (' 2014/12/25 '); YYYY/MM/DD
console.log (DT);//=> {DATE}:2014/12/25 00:00:00
dt = new Date (' 2014/12/25 12:00:00 ');//yyyy/ Mm/dd HH:mm:ss
console.log (DT);//=> {DATE}:2014/12/25 12:00:00
 
dt = new Date (' 2014-12-25 ');//yyyy-mm-dd< C4/>console.log (DT); => {date}:2014-12-25 08:00:00 (plus the time zone of East 8)
dt = new Date (' 2014-12-25 12:00:00 ');//Yyyy-mm-dd HH:mm:ss (Note: This conversion There will be an error in IE. )
console.log (DT);//=> {date}:2014-12-25 12:00:00
2, Get method

getFullYear (): Returns the year value of the Date object; 4-bit year.
GetMonth (): Returns the month value of the Date object. Starting from 0, so the real month = Returns the value +1.
GetDate (): Returns the date value in the month of the Date object, 1~31 the range of the value.
GetHours (): Returns a Date object's small value.
Getminutes (): Returns the minute value of a Date object.
Getseconds (): Returns the number of seconds for a Date object.
Getmilliseconds (): Returns the millisecond value of the Date object.
Getday (): Returns the week value of the week for the date object, 0 for Sunday, 1 for Monday, 2 for Tuesday, and so on
GetTime (): Returns the millisecond value between the Date object and ' 1970/01/01 00:00:00 ' (The time zone in Beijing is East 8, and the starting time is actually: ' 1970/01/01 08:00:00 '). 3. Set Method

setFullYear (year, Opt_month, opt_date): Sets the Date object's age value, 4-bit year.
Setmonth (Month, opt_date): Sets the month value of the Date object. 0 indicates January, 11 represents December.
Setdate (date): Sets the date value in the month of the date object; 1~31 the range of the value.
Sethours (Hour, opt_min, Opt_sec, opt_msec): Sets the Date object's small values.
Setminutes (Min, Opt_sec, opt_msec): Sets the minute value of the Date object.
Setseconds (sec, opt_msec): Sets the number of seconds for a Date object.
Setmilliseconds (msec): Sets the millisecond value of the Date object.

var dt = new Date ();
Dt.setfullyear (2014); => 2014: Year
dt.setmonth (one);//=> 11: Month; actual for December (month starting from 0)
dt.setdate;//=> 25: Day
dt.sethours (15); => 15:
dt.setminutes//=> 30: Min
dt.setseconds (s);//=> 40: Sec
dt.setmilliseconds (333); => 333: Millisecond
console.log (DT);//=>  December 25, 2014 15:30 40 second 333 ms
4, Date.parse (DATESTR)

Description: Converts a string to a Date object and returns the millisecond value between this Date object and ' 1970/01/01 00:00:00 ' (The time zone in Beijing is East 8, and the starting time is actually: ' 1970/01/01 08:00:00 ')
Parameters:
①datestr {string}: A string that can be converted to a Date object (can be omitted); There are two main types of strings:
1) yyyy/mm/dd HH:mm:ss: If the time is omitted, the time of the date object returned is 00:00:00.
2) Yyyy-mm-dd HH:mm:ss: If the time is omitted, the Date object returned is 08:00:00 (plus the local time zone). If you do not omit the time, this string returns Nan (not a number) in IE!
return value:
{int} returns the number of milliseconds between the converted Date object and the start time.
5, new Date (year, month, Opt_day, Opt_hours, Opt_minutes, Opt_seconds, Opt_milliseconds)

Convert Date To Date object
Parameters:
①year {int}: year; 4 digits. such as: 1999, 2014
②month {int}: month; 2 digits. Starting from 0, 0 means January, 11 represents December.
③opt_day {int} is optional: number, 2 digits, starting from 1, 1 represents 1th.
④opt_hours {int} optional: time; 2 digits; Value 0~23.
⑤opt_minutes {int} optional: 2 digits; 0~59.
⑥opt_seconds {int} optional: seconds; 2 not digits; 0~59 value.
⑦opt_milliseconds {int} is optional: milliseconds; 0~999 value.
return value:
{Date} returns a converted Date object.

var dt = new Date (2014, 11); December 2014 (The number of months entered here is one)
console.log (DT);//=> {date}:2014/12/01 00:00:00
dt = new Date (2014, 11, 25);//201 4 year December 25
console.log (DT);//=> {DATE}:2014/12/25 00:00:00
dt = new Date (2014, 11, 25, 15, 30, 40);//December 2014 25th 15:30 40 seconds
console.log (DT);//=> {DATE}:2014/12/25 15:30:40
dt = new Date (2014, 12, 25);//March 25, 2014 (This is lost The number of months entered is 12, which represents the 13th month, and jumps to the second year of January)
console.log (DT);//=> {DATE}:2015/01/25


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.