Js-20170816-date Object

Source: Internet
Author: User

1. The concept date object can be called directly as a normal function, returning a string representing the current time. Date ();//"Wed 19:37:37 gmt+0800 (CST)" Note that when date is used as a normal function, even with parameters, the current time is returned. Date (1, 1);//"Wed-19:37:37 gmt+0800 (CST)" 2. The New date () Date can also be used as a constructor. Using the new command on it returns an instance of a Date object. If you do not add an argument, the object that represents the current time is generated. As a constructor, a date object can accept parameters in multiple formats. (1) The new date (milliseconds) Date object accepts the number of milliseconds since January 1, 1970 00:00:00 UTC as the parameter. This means that if you use a Unix timestamp (in seconds) as a parameter, you must multiply the Unix timestamp by 1000. The argument to the date constructor can be a negative number that represents the time before January 1, 1970. (2) The new date (datestring) Date object also accepts a date string as a parameter, returning the corresponding time. The full format of the date string is "Month day, year Hours:minutes:seconds", for example " 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.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) 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. (3) New date (year, month [, Day, hours, minutes, seconds, Ms]) The date object can also accept more than one integer as a parameter, which in turn represents years, months, days, hours, minutes, seconds, and milliseconds. 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. 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.  The parameter can also use a negative number to indicate the time of the deduction. Static methods for 3.Date Objects 3.1 Date.now ()The Date.now method returns the current number of milliseconds from January 1, 1970 00:00:00 UTC (Unix timestamp multiplied by 1000). 4.Date Instance Object method Date instance object, there are dozens of own methods, divided into the following three categories.to class: Returns a string from the Date object that represents the specified time. Get class: Gets the date and time of the Date object. Set class: Sets the date and time of the Date object. 4.1 to class method (1) Date.prototype.toString ()The ToString method returns a full date string. var d = new Date (0, 1);d. toString ()//"Tue Jan 00:00:00 gmt+0800 (CST)" d//"Tue Jan 00:00:00 0 (CST) "because ToString is the default calling method, it is equivalent to calling this method if you are reading a Date object instance directly. (2) Date.prototype.toUTCString ()The toUTCString method returns the corresponding UTC time, which is 8 hours later than Beijing time. var d = new Date (2013, 0, 1); D.toutcstring ()//"Mon, 16:00:00 GMT" d.tostring ()//"Tue Jan 00:00:00 (CST)" (3) gmt+0800 Pe.tolocaledatestring ()The toLocaleDateString method returns a string that represents the local spelling of the date. var d = new Date (7, +);d. tolocaledatestring ();//Chinese version of the browser is "August 17, 2017"//English browser for "2017/8/17" 4.2 The Get class method Date object provides a series of get* methods to get the value of an aspect of an instance object. GetTime (): Returns the number of milliseconds from 00:00:00 January 1, 1970, equivalent to the ValueOf method. GetDate (): Returns the number of month (starting from 1) that the instance object corresponds to. GetDay (): Returns the day of the week, Sunday is 0, Monday is 1, and so on. GetYear (): Returns the number of years from 1900. getFullYear (): Returns the four-bit year. GetMonth (): Returns the month (0 = January, 11 for December). GetHours (): Returns the hour (0-23). Getmilliseconds (): Returns the millisecond (0-999). Getminutes (): Returns the minute (0-59). Getseconds (): Returns seconds (0-59). getTimezoneOffset (): Returns the time zone difference between the current time and UTC, expressed in minutes, and the return result takes into account the daylight savings factor. var d = new Date (' January 6, 2013 '); D.getdate ()//6d.getmonth ()//0d.getyear ()//113d.getfullyear ()//2013d.gettimezoneoffset ()//-480 in the above code, the last line returns-480, Indicates that UTC is 480 minutes later than the current time, or 8 hours.Here's an example of how many days are left in the year. function Leftdays () {var today = new Date ();var endyear = new Date (Today.getfullyear (), one, up, up, up, 999);var msperday = * + * + *;return Math.Round ((Endyear.gettime ()-today.gettime ())/msperday);}The 4.3 Date.prototype.valueOf () ValueOf method returns the number of milliseconds that the instance object is corresponding to the January 1, 1970 00:00:00 UTC, which is equivalent to the GetTime method. var d = new Date (); D.valueof ()//1502942893110d.gettime ()//1502942893110

Js-20170816-date Object

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.