Introduction to the moment of date types in JavaScript

Source: Internet
Author: User
Tags time zones

about the date type in JavaScript, I believe that jser are not unfamiliar with it, but also necessarily for that complex difficult to remember the various conversion functions headache, this article will share my knowledge of the date type of JS small summary, And the easy to make mistakes in the place to point out, at the same time introduction and promotion moment.js This JS library, I hope you read the article after the date class to deal with a duck.

1 uniqueness and diversity of time

A moment in any part of the world should be unique, the time zone is different in order to make the Earth in different time zones of the people's 12 o'clock Noon is the sun is on the head, the formation of communication is not so many obstacles. This standard is known as Greenwich Mean Time (Greenwich Mean, also known as the world, referred to as GMT), GMT time accuracy is a few milliseconds per day, in this case, Coordinated Universal Time (Coordinated U niversal T IME, referredto as UTC, appears with an accuracy of nanoseconds per day. UTC Saves the date by the number of milliseconds that begin at midnight (0 o'clock) on January 1, 1970. The advent of the standard does not make our JavaScript easy for time processing, because the world time zones are different, so whether you provide UTC time or local time is important to the result of processing, and the same results show UTC time or local time is a different time zone.

2 Converting known information to date class argument in constructor new Date ()

Date constructor without passing parameters, the newly created object automatically obtains the current date and time. If you want to create a Date object based on a specific date and time, you must pass in the number of milliseconds that represent that date (that is, the number of milliseconds from midnight UTC January 1, 1970 to that date). Therefore, the new Date (0) is the UTC time of January 1, 1970 00:00:00.000, but if passed in as a string representing time, it will automatically call the incoming ECMAScript Date.parse () method to convert the string to the corresponding distance GMT standard Time the number of milliseconds, such as new Date ("May 25, 2004"), and new Date (Date.parse ("May 25, 2004") are created at the same time as the date class . However, Date.parse () The behavior of this method varies by implementation, and usually varies by region, so the cross-domain cross-platform of code is a hidden danger and is not recommended.

        like imitation date.parse (), the date constructor also mimics DATE.UTC (), and the DATE.UTC () method also returns the number of milliseconds that represent the date, But it uses different information with date.parse () when building values. The arguments for DATE.UTC () are the year, the month based on 0 (January is 0, February is 1, and so on), the Day of the month (1 to 31), the number of hours (0 to 23), the minute, the second, and the number of milliseconds. Of these parameters, only the first two parameters (year and month) are required. If the number of days in the month is not provided, the number of days is assumed to be 1, and if other parameters are omitted, all are assumed to be 0. Here are two examples of using the DATE.UTC () method:

       var y2k = new Date (DATE.UTC (2000, 0));                          ,         &NB Sp            //GMT January 1, 2000 midnight 0 O'Clock
/ span>        var allfives = new Date (DATE.UTC (2005, 4, 5, 17, 55, 55));                    //GMT May 5, 2005 1 7:55:55

        but is not new Date ( DATE.UTC (2005, 4, 5, +,,) ) and new Date (2005, 4, 5, +, +, + ) created with new date ( 2005, 4, 5, +, 55" created is local time May 5, 2005 17:55:55, if the Code execution Environment is China (gmt+08), the corresponding is to create gmt time May 5, 2005 09:55:55, so many times we will make mistakes here.

Summing up, the new Date () constructor method is error-prone, and the support format for strings is Limited, for example, I get the string "12-10-27 09:00:00 PM", we need to convert it to a new one step at a The string in Date () is also a rather cumbersome process, so I recommend that you use Moment.js.

Moment.jsPass the reference

        Actually moment.js is a class that inherits the date constructor, so the rules for the new date () above are OK for it. For example, the above "12-10-27 09:00:00 PM", we can use Moment (" 12-10-27 09:00:00 PM ",  " Yy-mm-dd hh-mm-ss A ") is OK. It is important to note that the moment.js is also unavoidable in the event that the local time or GMT time is present, and if there is no time zone information for the string that is not in the argument, it will be processed in local time, so as long as you pay attention to this, it greatly simplifies our work.




3 Converting the date class to target information

 the method of the native date class is enough to convert a date class into a variety of information, Of course, if the moment.js generated by the above-mentioned moment class, its conversion string format method is only more than the original date class method, here is just the native method introduced.
      ToString                              -------& nbsp; (New Date ()). ToString ()
                                   ,         &NB Sp      -------"Sun Apr 15:44:02 gmt+0800 (?? -???? ????? -?é-′) "   //output information varies by region and browser
        toUTCString                          -------(new Date ()) . toutcstring ()
                                                   --- ----"Sun, APR 07:42:41 GMT"                         and nbsp                  //Output Information Format constant

· getTimezoneOffset ()-------returns the number of minutes that the local time differs from UTC time. For example, US Eastern Standard Time returns 300. Go to daylight saving time in somewhere
, this value will change.
gettime () ------- Returns the number of milliseconds representing the date, the same as the value returned by the valueof () method
· SetTime (ms) ------- Set the date in milliseconds and change the entire date
getfullyear () ------- get 4-digit years (such as 2007 instead of just 07)
getutcfullyear () ------- returns the 4-digit year of the UTC date
setFullYear (years) ------- Set the year of the date. The incoming year value must be a 4-digit number (such as 2007 instead of just 07)
· setUTCFullYear (year) ------- Set the year of the UTC date. The incoming year value must be a 4-digit number (such as 2007 instead of just 07)
getmonth () ------- Returns the month of the date, where 0 represents January, and 11 represents December
getutcmonth () ------- Returns the month of the UTC date, where 0 represents January, and 11 represents December
setmonth (month) ------- the month in which the date is set. The incoming month value must be greater than 0, and more than 11 increases the year
· setUTCMonth (month) ------- the month in which the UTC date is set. The incoming month value must be greater than 0, and more than 11 increases the year
getdate () ------- Returns the number of days in the date month (1 to 31)
getutcdate () ------- Returns the number of days in the UTC date month (1 to 31)
· SetDate (day) ------- Set the number of days in the date month. If the value passed in exceeds the number of days that should be in the month, increase the month
setutcdate (day) ------- Set the number of days in the UTC date month. If the value passed in exceeds the number of days that should be in the month, increase the month
getday () ------- Returns the day of the week (where 0 represents Sunday, 6 is Saturday)
· GetUTCDay () ------- Returns the day of the week in UTC (where 0 is Sunday, 6 means Saturday)
gethours () ------- number of hours in the return date (0 to 23)
getutchours () ------- Returns the number of hours in UTC date (0 to 23)
· Sethours (time) ------- Set the number of hours in the date. Increase the number of days in the month if the value passed in exceeds 23
setutchours (time) ------- Set the number of hours in the UTC date. Increase the number of days in the month if the value passed in exceeds 23
getminutes () ------- The number of minutes in the return date (0 to 59)
getutcminutes () ------- Returns the number of minutes in UTC date (0 to 59)
· Setminutes (min) ------- The number of minutes in the set date. Increase the number of hours when the value passed in exceeds 59
setutcminutes (min) ------- Set the number of minutes in the UTC date. Increase the number of hours when the value passed in exceeds 59
getseconds () ------- The number of seconds in the return date (0 to 59)
getutcseconds () ------- Returns the number of seconds in UTC date (0 to 59)
· setseconds (seconds) ------- The number of seconds in the set date. An incoming value greater than 59 increases the number of minutes
setutcseconds (seconds) ------- Sets the number of seconds in the UTC date. An incoming value greater than 59 increases the number of minutes
getmilliseconds () ------- The number of milliseconds in the return date
getutcmilliseconds () ------- Returns the number of milliseconds in the UTC date
· Setmilliseconds (milliseconds) ------- Set the number of milliseconds in the date
setutcmilliseconds (milliseconds)-------set the number of milliseconds in the UTC date

Introduction to the moment of date types in JavaScript

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.