The format required by JavaScript For the date conversion function parse () is xxxx/XX/xx
Eg:
VaR RegEx = new Regexp ("\-", "Gi ");
Timehopestart = timehopestart. Replace (RegEx ,"/");
If (new date (date. parse (timehopestart) <new date ()){
Alert ("the expected start date must be greater than or equal to the current date! ");
Return false;
}
Online SectionCodeAs follows:
Window. onload = function (){
VaR dependedval = "2005-3-4 ";
// Convert a date string to a date
VaR RegEx = new Regexp ("\-", "Gi ");
Dependedval = dependedval. Replace (RegEx ,"/");
// Dependedval = dependedval. Replace ("\-", "/"); // This does not work
Alert (dependedval)
// The format 2005/3/4 is required for parse! Falk!
VaR milliseconds = date. parse (dependedval );
Alert (milliseconds)
VaR dependeddate = new date ();
Dependeddate. settime (milliseconds );
VaR now = new date ();
// Note the brackets. The priority is not correct.
Alert ("Years separated:" + (now. getfullyear ()-dependeddate. getfullyear ()));
}
Additional JS date knowledge:
--------------------------------------------------------------------
Create a date object:
VaR objdate = new date ([arguments list]);
The following five parameters are supported:
View plainnew date ("month DD, yyyy hh: mm: SS ");
New date ("month DD, YYYY ");
New Date (yyyy, MTH, DD, HH, mm, SS );
New Date (yyyy, MTH, DD );
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: Number of milliseconds, an integer greater than or equal to 0,It indicates the number of milliseconds between the creation time and the GMT time on January 1, January 1, 1970.
I found:
The construction of dates in javascript can also support new date ("yyyy/mm/DD"); where: mm isAn integer indicates the month.From 0 (January) to 11 (December), the regular expression can be used to easily convert the string date.
Test code:
<MCE: Script Type = "text/JavaScript"> <! --
Document. Write ("<br/>" + new date ("February 3, 2009 "));
Document. Write ("<br/>" + new date ("February 10:52:03 "));
Document. Write ("<br/> ");
Document. Write ("<br/>" + new date (2009, 1, 3 ));
Document. Write ("<br/>" + new date (2009, 1 ));
Document. Write ("<br/> ");
Document. Write ("<br/>" + new date (date. parse ("February 3, 2009 ")));
Document. Write ("<br/>" + new date (date. parse ("February 10:52:03 ")));
Document. Write ("<br/>" + new date (date. parse (2009, 1, 3); // output: Nan
Document. Write ("<br/>" + new date (date. parse (,); // output: Nan
Document. Write ("<br/>" + new date (date. parse ("2009/02/03 ")));
Document. Write ("<br/> ");
Document. Write ("<br/>" + new date ("2009/02/03 "));
Document. Write ("<br/>" + new date ("2009/02/03 11:12:13 "));
Document. Write ("<br/>" + new date ("2009-02-03"); // output: Nan
// --> </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