The js string date yyyy mm dd is converted to the date sample code

  • 2020-03-30 02:14:47
  • OfStack

A recent problem occurred when the date.parse (STR) function reported an error: NAN under ff when getting the Date from the form and passing it to the background via json

I found out that it was due to the date.parse () function that requires a Date format: refer to the date.parse function for details

For js operation date:

Create a date object:

Var objDate = new Date ([] the arguments list);

There are five parameter forms 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); 

Description:

Month: December, December, December, December, December, December
MTH: an integer for months, from 0 (January) to 11 (December)
The Content

Dd: represents the days of the month, from 1 to 31
Yyyy: the year represented by four digits
Hh: hours, from 0 (midnight) to 23 (11 PM)
Mm: minutes, integers from 0 to 59
Ss: number of seconds, integers from 0 to 59
Ms: the number of milliseconds, an integer greater than or equal to 0, representing the number of milliseconds between the time to be created and the date of January 1, 1970 GMT.

I found:

The construction of a Date in Javascript can also support new Date("yyyy/MM/dd"). Where: MM is an integer for the month from 0 (January) to 11 (December), making it easy to convert string dates using regular expressions.

Test code:
 
<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 results:

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
-------------------
 
window.onload=function(){ 
var dependedVal="2005-3-4"; 
//Converts to a date based on a date string
var regEx = new RegExp("\-","gi"); 
dependedVal=dependedVal.replace(regEx,"/"); 
//dependedVal=dependedVal.replace("\-","/");// So I can't  
alert(dependedVal) 
//Parse requires the format 2005/3/4
var milliseconds=Date.parse(dependedVal); 
alert(milliseconds) 
var dependedDate=new Date(); 
dependedDate.setTime(milliseconds); 

var now = new Date(); 
//Notice the parenthesis, the priority problem, the frustration
alert(" Years apart :"+(now.getFullYear() - dependedDate.getFullYear())); 
} 

In fact, the date between the browser and the server must be transmitted by the value of milliseconds, otherwise you will report an error of 400!

Related articles: