Date () Date creation

Source: Internet
Author: User
Tags string format

Ah .. People become lazy these months... Write more blogs in the future.

A problem was reported during the test last Friday, saying that there was a problem with date calculation. The original two dates differ by one day, but the system calculates a difference of 31 days. (Friday, January 1, October 31, 2014)

Later, I checked the cause. This is what a brother wrote in front-end JavaScript code.

The function is to create a time object for '2017-11-03 23:59:59.

Var expirationDateStr = '2017-11-03 23:59:59 '; var expirationDate = new Date (); expirationDate. setFullYear (parseInt (expirationDateStr. substr (0, 4), 10); expirationDate. setMonth (parseInt (expirationDateStr. substr (5, 2), 10)-1); expirationDate. setDate (parseInt (expirationDateStr. substr (8, 2), 10); expirationDate. setHours (parseInt (expirationDateStr. substr (11, 2), 10); expirationDate. setMinutes (parseInt (expirationDateStr. substr (14, 2), 10); expirationDate. setSeconds (parseInt (expirationDateStr. substr (17, 2), 10 ));

The second sentence is a new Date object, which is created as the current time object of the system. Assume that the current time is 17:30:00,

The expirationDate printed on the console is: Fri Oct 31 23:59:59 UTC + 0800 2014 (23:59:59 ).

The last six sentences: Set the time to replace the year, month, day, hour, minute, and second in expirationDateStr with the obtained system time. (ParseInt (?, 10) What is the role? Convert to a decimal int integer, substr () string truncation method)

On the console, we can print the replaced expirationDate (console.info (expirationDate ))

Ideally, Mon Nov 3 23:59:59 UTC + 0800 2014 (23:59:59)
However, Wed Dec 3 23:59:59 UTC + 0800 2014 (23:59:59)

Later, I was very depressed. I checked the js api and thought it was okay. setFullYear (); setMonth (); setDate (); it was indeed used in this way .. Think about whether it is because the input parameter is not an int, and later found that it is not.

Why is this happening?

Analyze

Current date "17:30:00"

Then: setFullYear (2014) No problem, or 17:30:00

A problem occurs in setMonth (10). The date is changed to "17:30:00" because setMonth (10) means that the date is changed to "17:30:00", but this date does not exist, date is automatically recognized and converted. If there is only 30 days on January 1, November, the month is changed to 12, and the day is 1... The last setMonth (10) is to convert "November 17:30:00" to "17:30:00" (the setMonth () parameter is 0-11, and 10 is)

SetDate (03) because the date of the month has changed to "17:30:00", the date is "17:30:00 ".

There is no error in the subsequent hours, minutes, and seconds, that is, the following message is displayed: the date after the conversion is printed on the console: Wed Dec 3 23:59:59 UTC + 0800 (2014 23:59:59)

 

Therefore, to implement the above functions, you cannot directly use setFullYear (); setMonth (); setDate (); to replace the year, month, and day of the current date. (This problem also occurs in java)

 

 

So how to set it involves Date () initialization.

In js APIs, Date () can be created as follows:

Var d = new Date (); var d = new Date (milliseconds); var d = new Date (dateString); var d = new Date (year, month, day, hours, minutes, seconds, milliseconds );

 

It seems that the API is not so detailed ....

 

For the four methods, I only say the third new Date (dateString)

Var str1 = "November 06, 2014 15:30:00"; var str2 = "06-11-2014 15:30:00"; var str3 = "2014/11/06 15:30:00"; var str4 = "06/11/2014 15:30:00"; var str5 = "15:30:00 "; var str6 = "November 20 15:30:00"; console.info ("******** create 15:30:00 date object test *******"); console.info (str1 + ":" + new Date (str1); console.info (str2 + ":" + new Date (str2); console.info (str3 + ": "+ new Date (str3); console.info (str4 +": "+ new Date (str4); console.info (str5 +": "+ new Date (str5 )); console.info (str6 + ":" + new Date (str6); console.info ("***** correct: thu Nov 6 15:30:00 UTC + 0800 2014 *****");


Test the js result in the console as follows:

* ****** Create a 15:30:00 Date object test ****** 15: 30: 00: Invalid Date 06-11-2014 15: 30: 00: wed Jun 11 15:30:00 UTC + 0800 2014 15: 30: 00: Thu Nov 6 15:30:00 UTC + 0800 2014 06/11/2014 15: 30: 00: wed Jun 11 15:30:00 UTC + 0800 2014 November 06, 2014 15: 30: 00: Invalid Date 06, December 31, November 20-: 00: Invalid Date ***** correct: thu Nov 6 15:30:00 UTC + 0800 2014 *****

We can see that the format of dateString is var str3 = "15:30:00"; that is, the date is separated by the slash "/" to: yyyy/MM/dd hh: mm: ss

Although I do not know there are other formats, I found that new Date ("15:30:00"); can also create an object with a Date of 15:30:00.

 

In this case. If you want to implement the function of starting code, you can use the following code:

Var expirationDateStr = '2017-11-03 23:59:59 '; var expirationDate = new Date (expirationDateStr. replace (/-/g ,"/"));

 

You can use two codes. Replace. Replace (/-/g, "/") means to replace "-" in the string with "/". After replacement, the string format is 23:59:59, and g indicates global replacement, if no g is added, replace will replace only one "-" at a time.


 

 

 

Date () Date creation

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.