Javascript reference type time Date
Create date object
In javascript, you can use the Date() constructor to Create a date object, such as:
var date=new Date();
When no date parameter is passed to the constructor, an object with the current date and time will be created.
Of course, if you want to create a date object based on a specific date and time, you can also do so. You just need to pass the parameters that can represent the date into the constructor.
The common date formats accepted by the Date() constructor are:
"month/day/year", such as 2/27/2014;
"English month Name day, year ", such as February 27, 2014;
" year, month, day, hour, minute, second, millisecond ", such as 2014,1,27,11,22,22
The following creates a date object in the above format:
var date1=new Date("2/27/2014"); alert(date1); //Thu Feb 27 2014 00:00:00 GMT+0800 var date2=new Date("February 27,2014"); alert(date2); //Thu Feb 27 2014 00:00:00 GMT+0800 var date3=new Date(2014,1,27,11,24,0); alert(date3); //Thu Feb 27 2014 11:24:00 GMT+0800 var date4=new Date(2014,1,27); alert(date4); //Thu Feb 27 2014 00:00:00 GMT+0800 var date5=new Date("2014,1,27,11,24,0"); alert(date5); //Invalid Date
Through the above example, you may notice the difference between them:
First, when creating a date object using the first two methods, it must be passed in as a string as a parameter; when using the third method, it cannot be passed in as a string. Each value must be passed in as a separate value.
Second, one thing that must be paid special attention to is that when using the third method to create a date, its month starts from 0, that is, January corresponds to 0, and so on; while for the first two methods, It is a normal month representation, that is, February corresponds to 2.
Third, when using the third way to express, the year and month are required, and when other parameters are omitted, they will be expressed as 0.
Note: The first two methods will get the same result as the displayed calling Date.parse() method; the third method will get the same result as the displayed calling Date.UTC() method.
Inherited methods
The Date type also inherits the toString(), toLocaleString() and valueOf() methods. The format of the values obtained by calling these methods will vary between browsers. Specifically, you can try calling it yourself.
Date formatting methods
The Date type also has some methods specifically used to format dates into strings, as follows:
toDateString( )—Displays the day of the week, month, day, and year in an implementation-specific format;
toTimeString()—Displays the hours, minutes, seconds, and time zone in an implementation-specific format;
toLocaleDateString() - Display the day of the week, month, day, year in a region-specific format;
toLocaleTimeString() - Display in a reality-specific format , minutes, seconds;
toUTCString() - displays the complete UTC day in a display-specific format
The above is the detailed content of Introduction to creating date objects and date formatting methods in javascript. For more information, please follow other related articles on the PHP Chinese website!