Home > Web Front-end > JS Tutorial > body text

Convert js string date yyyy-MM-dd to date sample code_javascript skills

WBOY
Release: 2016-05-16 16:56:49
Original
1436 people have browsed it

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 looking for some information, I found that the reason is The Date.parse() function has requirements for date format: For details, refer to the Date.parse function

For js operation date:

Create a date object:

var objDate=new Date([arguments list]);

The parameter forms are as follows:

Copy code 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);

Description:

month: Represents the month name in English, from January to December
mth: Represents the month as an integer, from 0 (January) to 11 (December)
Content

dd: Represents the first day of the month Days, from 1 to 31
yyyy: Four-digit year
hh: Hours, from 0 (midnight) to 23 (11 p.m.)
mm: Minutes, from 0 to 59 Integer
ss: number of seconds, integer from 0 to 59
ms: number of milliseconds, an integer greater than or equal to 0, indicating the difference between the time to be created and GMT time January 1, 1970 number of milliseconds.

I found out:

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 to convert string dates.

Test code:
Copy code The code is as follows:



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 code The code is as follows:

window .onload=function(){
var dependentVal="2005-3-4";
//Convert to date based on date string
var regEx = new RegExp("\-","gi" ;
//parse requires the format of 2005/3/4
var milliseconds=Date.parse(dependedVal);
alert(milliseconds)
var dependentDate=new Date();
dependedDate .setTime(milliseconds);

var now = new Date();
//Pay attention to the brackets, priority issues, helplessness
alert("Years apart:" (now.getFullYear() - dependedDate.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!
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template