Create a Date object that uses the new operator followed by the constructor of date.
var date = new Date();
When the default constructor is called, the newly created date automatically obtains the current time and date. If you need to specify a date and time, you need to pass in the number of milliseconds that represent that date.
There are two methods available in JavaScript to calculate the date, and the Date.parse () method receives a string parameter that represents the date, and then returns the corresponding number of milliseconds based on that date. However, the format of dates is often different depending on the implementation and region. DATE.UTC () also returns the number of milliseconds that represent the date, with the parameters of the year, the month based on 0 (January is 0), the day of the month, the number of hours (0 to 23), the minute, the second, and the number of milliseconds. the year and month two parameters are required.
var date = new Date(Date.parse("May 1, 2016"));// GMT时间2016年1月1日凌晨0点var date = new Date(Date.UTC(2016,0));// GMT时间2016年5月10日 21:46:30var date1 = new Date(2016,5,10,21,46,30);
1. Methods of inheritance
- tolocalestring (): returns the date and time in a format that is compatible with the locale of the browser. The time format will contain am or PM, but will not contain time zone information
- toString (): returns the date and time with time zone information, typically military time (the range of hours is 0 to 23).
valueOf (): does not return a string, but instead returns the number of milliseconds of the date. Comparison operators can be used to compare
var date1 = new Date (2016, 1, 9); var date2 = new Date (2016, 5, 10); Alert (Date1 < DATE2); True
2. Method of date formatting
The date type provides some methods for formatting dates as strings:
- todatestring () displays weekday, month, day, and year in a specific implementation format
- totimestring () displays hours, minutes, seconds, and time zones in an implementation-specific format
- tolocaledatestring () displays weekday, month, day, and year in a locale-specific format
- tolocaletimestring () displays hours, minutes, seconds in an implementation-specific format
- toutcstring () full UTC date in implementation-specific format
3. Date/Time component method
- getTime (): Returns the number of milliseconds that represent the date
- settime (): Set the date in milliseconds
- getMonth (): Returns the month of the date, where 0 indicates a one month
- GetDay (): Returns the day of the week (0 = Sunday, 6 for Saturday)
- getHours (): Returns the number of hours in a date (0 to 23)
- getminutes (): Returns the number of minutes in a date (0 to 59)
- getseconds (): Returns the number of seconds in a date (0 to 59)
You can view the document as needed.
Javascript:date type