Sample Code for converting js string date yyyy-MM-dd to date
When retrieving the Date in the form and passing it to the background in json mode, the Date. parse (str) function reports an error in ff. If you have any similar situations, refer to this article.
Recently, I encountered a problem where the Date. parse (str) function encountered an error in ff When retrieving the Date in the form and passing it to the background in json format: NAN
I found that the Date. parse () function requires the Date format. For details, refer to the Date. parse function.
Js Operation Date:
Create a date object:
Var objDate = new Date ([arguments list]);
The following five parameters are supported:
The Code is as follows:
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: 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 can also support new Date ("yyyy/MM/dd"). MM indicates that the month ranges from 0 (January) to 11 (December ), in this way, the regular expression can be used to easily convert the string date.
Test code:
The Code is as follows:
<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
-------------------
The Code is as 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 ("-", "/"); // No
Alert (dependedVal)
// The format 2005/3/4 is required for parse.
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 ()));
}
In fact, the date must be transmitted between the browser and the server in milliseconds. Otherwise, an error 400 will be reported!