Create a date object, using 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 representing the date.
JavaScript provides two methods to calculate dates. The Date.parse() method receives a string parameter representing the date, and then returns the corresponding date milliseconds based on this date. However, the format of dates often varies by implementation and region. Date.UTC() also returns the number of milliseconds representing the date, and its parameters are year, month based on 0 (January is 0), day in the month, hours (0 to 23), minutes, seconds, and milliseconds. The two parameters of year and month are required.
var date = new Date(Date.parse("May 1, 2016"));// GMT time on January 1, 2016 at 0:00 am var date = new Date(Date.UTC(2016,0));// GMT time on May 10, 2016 at 21:46:30var date1 = new Date(2016,5,10,21,46,30);1. Inheritance method
• toLocaleString(): Returns the date and time in a format that is appropriate for the region set by 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, and the time is generally military time (the range of hours is 0 to 23).
• valueOf(): does not return a string, but returns the number of milliseconds of the date. You can use the comparison operator to compare
var date1 = new Date(2016, 1, 9); var date2 = new Date(2016, 5, 10); alert(date1 < date2); // true
2. Date formatting method
The Date type provides some methods for formatting dates into strings:
• toDateString() Displays the day of the week, month, day, and year in a specific implementation format
• toTimeString() Displays time, minutes, seconds, and time zones in implementation-specific formats
• toLocaleDateString() displays the day of the week, month, day, and year in a region-specific format
• toLocaleTimeString() displays time, minutes, and seconds in an implementation-specific format
• toUTCString() complete UTC date in implementation-specific format
3. Date/time component method
• getTime(): Returns the number of milliseconds representing the date
• setTime(): Set date in milliseconds
• getMonth(): Returns the month in the date, where 0 means January
• getDay(): Returns the day of the week in the date (0 means Sunday, 6 means Saturday)
• getHours(): Returns the number of hours in the date (0 to 23)
• getMinutes(): Returns the number of minutes in the date (0 to 59)
• getSeconds(): Returns the number of seconds in the date (0 to 59)
You can view the document when needed.
The above comprehensive analysis of JavaScript:Date type is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.