Create Date () and create date

Source: Internet
Author: User
Tags what date

Create Date () and create date

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 = '2014-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 = '2014-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.


 

 

 


Javascript Date object

Let's start from scratch:
1. var today; // This is the time object for creating a date.
2. var h; // use the today object to obtain the current hour of the system.
3. var m; // use the today object to obtain the current system minute
4. var s; // use the today object to obtain the current system second
5. checkTime (m); // call the checkTime () function and input the current minute to determine whether the current minute is less than 10. If yes, add 0 to the current minute. For example: returns the current minute
6. checkTime (s); // call the checkTime () function and pass in the current second to determine if the current second is less than 10. If yes, add 0 to the current minute. For example: 23: 01 minute 01 second returns current second
7. document. getElementById ('txt '). innerHTML = h + ":" + m + ":" + s
; // It means to search for the object with the Id of 'txt 'in the layer defined by the body, and then display the current time inside.
The answer is complete. I hope the landlord is satisfied ....

What Date formats does Date () in js support?

The Date object is used to process the Date and time.
Syntax for creating a Date object:
Var myDate = new Date ()
The Date object automatically saves the current Date and time as its initial value.
The following five parameters are supported:
New 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 that in the last form, the parameter represents the number of milliseconds between the creation time and the GMT time on January 1, January 1, 1970. The meanings of various functions are as follows:
Month: represents the name of a month in English, from January to December.
Mth: an integer representing the month, from (January 1, January) to 11 (January 1, December)
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: Number of milliseconds, an integer greater than or equal to 0
For example:
New Date ("January 12,2006 22:19:35 ");
New Date ("January 12,2006 ");
New Date );
New Date (2007,0, 12 );
New Date (1137075575000 );

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.