JS string date Yyyy-mm-dd converted to date sample code _javascript tips

Source: Internet
Author: User
Tags month name
Recently encountered a problem, is to get the date in the form back to pass through the JSON way, encountered Date.parse (str) function under FF error: NAN

I found some information. Date.parse () function is required for date format: detailed reference Date.parse function

For JS operation Date:

Create a Date object:

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

The following 5 kinds of parameter form are:
Copy 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);

Description

Month: Month name in English, from January to December
MTH: Month by integer, from 0 (January) to 11 (December)
Content

DD: Represents the day ordinal of one months, from 1 to 31
YYYY: four-digit year
HH: Hours, from 0 (midnight) to 23 (11 o'clock in the evening)
MM: Number of 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, represents the number of milliseconds to create and the difference between GMT time January 1, 1970.

I have found:

The construction of dates in JavaScript can also support new date ("Yyyy/mm/dd"); Where: MM is an integer representing the month from 0 (January) to 11 (December), which makes it easy to convert the string date by using regular expressions.

Test code:
Copy 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 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
-------------------
Copy Code code as follows:

Window.onload=function () {
var dependedval= "2005-3-4";
Convert from date string to date
var regEx = new RegExp ("\\-", "GI");
Dependedval=dependedval.replace (RegEx, "/");
Dependedval=dependedval.replace ("\\-", "/");
Alert (Dependedval)
Parse need 2005/3/4 this format
var milliseconds=date.parse (Dependedval);
Alert (milliseconds)
var dependeddate=new Date ();
Dependeddate.settime (milliseconds);

var now = new Date ();
Note parentheses, priority issues, helpless
Alert ("Years apart:" + (Now.getfullyear ()-dependeddate.getfullyear ());
}

In fact, the date between the browser and the server transmission between the millisecond value to pass, otherwise it will error 400 errors!

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.