I am working on a project today and have encountered a need to convert a date string (for example,) into a date object in Javascript. I am confused about the lack of JavaScript knowledge and the four parts, I checked some information online and got it out...
Go directly to the topic:
Create a date object:
VaR objdate = new date ([arguments list]);
The following five parameters are supported:
New date ("month DD, yyyy hh: mm: SS"); <br/> new date ("month DD, YYYY"); <br/> new date (yyyy, MTH, DD, HH, mm, SS); <br/> new date (yyyy, MTH, DD); <br/> new date (MS );
Note:
Month: represents the name of a month in English, from January to December.
MTH: indicates the month in an integer, from 0 (January 1, January) to 11 (January 1, December)
Content
DD: the day of a month, from 1 to 31.
Yyyy: The Year in four-digit format.
Hh: hours, from 0 (midnight) to 23)
MM: The number of minutes, an integer from 0 to 59.
SS: number of seconds, an integer from 0 to 59
MS: The number of milliseconds. The value is an integer greater than or equal to 0, indicating the difference between the creation time and GMT time on January 1, January 1, 1970.
I found:
The construction of dates in Javascript also supports the new date ("yyyy/mm/DD"), where: mm is an integer representing the monthFrom 0 (January) to 11 (December), the regular expression can be used to easily convert the string date.
Test code:
<MCE: Script Type = "text/JavaScript"> <! -- <Br/> document. write ("<br/>" + new date ("February 3, 2009"); <br/> document. write ("<br/>" + new date ("February 10:52:03"); <br/> document. write ("<br/>"); <br/> document. write ("<br/>" + new date (2009,1, 3); <br/> document. write ("<br/>" + new date (2009, 1,); <br/> document. write ("<br/>"); <br/> document. write ("<br/>" + new date (date. parse ("February 3, 2009"); <br/> document. write ("<br/>" + new date (date. parse ("February 10:52:03"); <br/> document. write ("<br/>" + new date (date. parse (2009, 1, 3); // output: Nan <br/> document. write ("<br/>" + new date (date. parse (2009, 1,); // output: Nan <br/> document. write ("<br/>" + new date (date. parse ("2009/02/03"); <br/> document. write ("<br/>"); <br/> document. write ("<br/>" + new date ("2009/02/03"); <br/> document. write ("<br/>" + new date ("2009/02/03 11:12:13"); <br/> document. write ("<br/>" + new date ("2009-02-03"); // output: Nan </P> <p> // --> </MCE: SCRIPT>
Output result:
Tue Feb 3 00:00:00 UTC + 0800 2009
Tue Feb 3 10:52:03 UTC + 0800 2009
Tue Feb 3 00:00:00 UTC + 0800 2009
Tue Feb 3 10:52:03 UTC + 0800 2009
Tue Feb 3 00:00:00 UTC + 0800 2009
Tue Feb 3 10:52:03 UTC + 0800 2009
Nan
Nan
Tue Feb 3 00:00:00 UTC + 0800 2009
Tue Feb 3 00:00:00 UTC + 0800 2009
Tue Feb 3 11:12:13 UTC + 0800 2009
Nan