JS time Date Object Introduction and solve the problem of gettime conversion to 8 points

Source: Internet
Author: User
Tags local time

Objective

When doing time conversion, it was found that the timestamp of "2016-04-12" was 08:00 of the time in 2016-04-12, not 0 points.

New Date ('2016-04-12'). GetTime (); // 1460419200000 New Date (1460419200000); // Tue Apr 08:00:00 gmt+0800

Finally, if the date format is replaced with "2016/04/12", then the normal conversion to 0 points.

New Date (new date ('2016/04/12'). GetTime ()); // Tue Apr 00:00:00 gmt+0800

Overview

  DateThe object is the operating interface for the date and time provided by JavaScript. It has many uses.

Inside JavaScript, all dates and times are stored as an integer that represents the number of milliseconds the current time is from 00:00:00 January 1, 1970, and the positive and negative range is 100 million days before and after the base time.

Date ()

As a function, a Date object can be called directly, returning a string of the current date and time.

Date () // "Tue Dec 09:34:43 gmt+0800 (CST)" Date (11) // "Tue Dec 09:34:43 gmt+0800 (CST)"

The above code shows that, regardless of parameters, calling date directly always returns the current time.

New Date ()

The Date object is also a constructor that uses the new command to return an instance of a Date object. If you do not add a parameter, the object that represents the current time is generated.

  

var New Date (); Today //  //  equivalent to today.tostring ()//  "Tue Dec 09:34:43 gmt+0800 (CST)" 

As a constructor, a date object can accept parameters in multiple formats.

  (1) New Date (milliseconds)

  The Date object accepts the number of milliseconds from January 1, 1970 00:00:00 UTC as the parameter. This means that if you use a Unix timestamp as an argument, you must multiply the Unix timestamp by 1000.

NewDate (1378218728000)//Tue Sep 22:32:08 gmt+0800 (CST)//January 2, 1970 at 0 o'clockvarjan02_1970 =NewDate (3600* -* +);//Fri Jan 1970 08:00:00 gmt+0800 (CST)//December 31, 1969 at 0 o'clockvardec31_1969 =NewDate (-3600* -* +);//Wed Dec 1969 08:00:00 gmt+0800 (CST)

The above code shows that the parameter of the date constructor can be a negative number that represents the time before January 1, 1970. Date objects can represent a range of dates that are 100 million days before and after January 1, 1970.

  (2) New Date (datestring)

The Date object also accepts a DateTime string as a parameter, returning the corresponding time.

New Date ("January 6,"); // Sun Jan 00:00:00 gmt+0800 (CST)

  

The full format of the date string is "Month day, year Hours:minutes:seconds", such as "December 25, 1995 13:30:00". If the hours, minutes, or seconds are omitted, the values are set to 0.

However, date strings in other formats can also be parsed. In fact, all date strings that can be parsed by the Date.parse () method can be used as arguments to the Date object

NewDate ("2013-2-15")NewDate ('2013/2/15')NewDate ("2013-feb-15")NewDate ("FEB, the")NewDate ("FEB,")NewDate ("Feberuary,")NewDate ("Feberuary,")NewDate ("Feberuary,") //Fri Feb 00:00:00 gmt+0800 (CST)

The above multiple date strings are written, returning the same time.

Note that, in ES5, if the date is separated by a conjunction line (-) format and has a leading 0,javascript that it is a date string in ISO format, the return time is calculated in the UTC time zone.

New Date ('2014-01-01')//  Wed Jan 08:00:00 gmt+0800 (CST) New Date ('2014-1-1')//  Wed Jan 00:00:00 gmt+0800 (CST) 

In the above code, the date string has no leading 0, and the returned result is not the same. If no leading 0,javascript engine assumes the user is in the local time zone, this example returns 0:0. If there is a leading 0 (that is, if you represent the date in ISO format), assume that the user is in the time zone at Greenwich International Standard, so return 8:0. However, ES6 changed this practice by stipulating that a date string that does not have a time zone specified is considered to be in the local time zone.

Date strings in other formats are treated as non-ISO and the local time zone is used as the timing standard.

New Date ('2014-12-11')//  Thu Dec 08:00:00 gmt+0800 (CST) New Date ('2014/12/11')//  Thu Dec 00:00:00 gmt+0800 (CST) 

In the above code, the first date string is in ISO format and the second is not.

  (3) New Date (year, month [, Day, hours, minutes, seconds, Ms])

 The Date object can also accept multiple integers as parameters, which in turn represent the year, month, day, hour, minute, second, and millisecond. If you use this format, you need to provide a minimum of two parameters (year and month), the other parameters are optional, and the default equals 0. Because if you use only the "year" parameter, the Date object interprets it as a number of milliseconds.

New Date ($)//  Thu Jan 1970 08:00:02 gmt+0800 (CST)

In the above code, 2013 is interpreted as the number of milliseconds, not the year.

In other cases, the omitted parameter is 0 by default.
  

NewDate ( -,0)//Tue Jan 00:00:00 gmt+0800 (CST) NewDate ( -,0,1)//Tue Jan 00:00:00 gmt+0800 (CST) NewDate ( -,0,1,0)//Tue Jan 00:00:00 gmt+0800 (CST) NewDate ( -,0,1,0,0,0,0)//Tue Jan 00:00:00 gmt+0800 (CST)

The above code (except the first line) returns the time of January 1, 2013 0 o'clock, and you can see that the month starts from 0, so January is 0, December is 11. However, the number of days in the month is calculated starting from 1.

These parameters are automatically converted if they are outside the normal range. For example, if the month is set to 15, it will be converted to April of the following year.

  

New Date ($)//  Tue Apr 00:00:00 gmt+0800 (CST)new Date ( 0,0)//  Mon Dec 00:00:00 gmt+0800 (CST)

The parameter can also use a negative number to indicate the time of the deduction.

New Date (1)//  Sat 00:00:00 gmt+0800 (CST)new Date (  - 0,-1)//  Sun Dec 00:00:00 gmt+0800 (CST)

The above code uses a negative number for the month and day respectively, indicating that the corresponding time is deducted from the base day.

Year is different, if 0, for 1900, or 1 for 1901, or if negative, BC.

New Date (00)//  Mon Jan 1900 00:00:00 gmt+0800 (CST)new Date (1  0)//  Tue Jan 1901 00:00:00 gmt+0800 (CST)new Date (-1
    0)//  Fri Jan 01-1 00:00:00 gmt+0800 (CST)
Operation of the date

When a type is cast, an instance of the Date object is equal to the corresponding number of milliseconds if it is converted to a numeric value, or the corresponding date string if the string is converted. Therefore, two date objects are subtracted, returning the number of milliseconds they are spaced, and the addition operation, which returns two strings after the connection.

var New Date (21); var New Date (31- d1//  2678400000  + d1//  "Sat Apr 00:00:00 gmt+0800 (CST) Wed Mar 00:00:00 (CST)"

JS time Date Object Introduction and solve the problem of gettime conversion to 8 points

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.