Recently I encountered a problem, that is, when getting the date in the form and transmitting it to the background through json, the Date.parse(str) function reported an error under ff: NAN
After searching for some information, I found that the Date.parse() function has requirements for the date format: please refer to the Date.parse function for details.
For js operation date:
Create a date object:
var objDate=new Date([arguments list]);
There are five types of parameter forms:
Copy the code code 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);
illustrate:
month: Indicates the name of the month in English, from January to December
mth: represents the month as an integer, from 0 (January) to 11 (December)
Content
dd: indicates the day of the month, from 1 to 31
yyyy: four-digit year
hh: hour, from 0 (midnight) to 23 (11 p.m.)
mm: Minutes, an integer from 0 to 59
ss: seconds, an integer from 0 to 59
ms: milliseconds, an integer greater than or equal to 0, indicating the number of milliseconds between the time to be created and January 1, 1970 GMT time.
What I found:
The date construction in Javascript can also support new Date("yyyy/MM/dd"); where: MM is an integer representing the month from 0 (January) to 11 (December), so it is very convenient to use regular expressions Ability to convert string dates.
Test code:
Copy the code code as follows:
<mce:script type="text/javascript"><!--
document.write("<br/>" + new Date("February 3,2009"));
document.write("<br/>" + new Date("February 3,2009 10:52:03"));
document.write("<br/>");
document.write("<br/>" + new Date(2009,1,3));
document.write("<br/>" + new Date(2009,1,3,10,52,03));
document.write("<br/>");
document.write("<br/>" + new Date(Date.parse("February 3,2009")));
document.write("<br/>" + new Date(Date.parse("February 3,2009 10:52:03")));
document.write("<br/>" + new Date(Date.parse(2009,1,3))); //Output: NAN
document.write("<br/>" + new Date(Date.parse(2009,1,3,10,52,03))); //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
-------------------
Copy the code code as follows:
window.onload=function(){
var dependentVal="2005-3-4";
//Convert to date based on date string
var regEx = new RegExp("//-","gi");
dependedVal=dependedVal.replace(regEx,"/");
//dependedVal=dependedVal.replace("//-","/");//This doesn't work
alert(dependedVal)
//parse requires the format of 2005/3/4
var milliseconds=Date.parse(dependedVal);
alert(milliseconds)
var dependentDate=new Date();
dependentDate.setTime(milliseconds);
var now = new Date();
//Pay attention to the brackets, priority issues, helplessness
alert("The number of years apart:"+(now.getFullYear() - dependentDate.getFullYear()));
}
In fact, the date must be transmitted between the browser and the server through the millisecond value, otherwise a 400 error will be reported!