Sample Code for converting js string date yyyy-MM-dd to date

Source: Internet
Author: User

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:
Copy codeThe 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:
Copy codeThe 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
-------------------
Copy codeThe 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 ("\-", "/"); // This does not work
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.