Js string date conversion from yyyy-MM-dd to date sample code _ javascript tips-js tutorial

Source: Internet
Author: User
When the Date in the form is retrieved and transmitted to the background in json format, the Date is returned. the parse (str) function reports an error in ff. If you have similar cases, you can refer to this article and recently encountered a problem, that is, when you get the date in the form and transmit it to the background in json format, date. the parse (str) function reports the following error in ff: 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:

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


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:

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
-------------------

The 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!
Related Article

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.