Some of the details used by the date function in JavaScript

Source: Internet
Author: User

For developers, date may sometimes be important, and we can create a date object from the new date (). For example:

The code is as follows Copy Code

var start = new Date (),//Get current time
Today = new Date (2013,6,6),//2013-7-6
tommorow = new Date (' 2013-7-7 '); 2013-7-7

In general, the above code should be very common, there is no special attention to the place, the only thing is:

The code is as follows Copy Code

New Date (2013,6,6); Second in the argument, the month needs to be plus 1.
New Date (' 2013-7-6 '); The month in the argument does not need to add 1

Sometimes, we might want to know the number of days ahead of the first day of the one month, such as I want to know the number of the day before March 1, 2013, so maybe write some statements like judgment, but this can be more easily calculated with date:

var pre = new Date (2013,2,0);

The result of the above is February 28, 2013, we do not need to write additional code to determine. Of course, we can also first create a time, and then set his date, such as:

The code is as follows Copy Code

var pre = new Date (2013,2,1);
Pre.tolocalestring ()//"3/1/2013 12:00:00 AM"
Pre.setdate (Pre.getdate ()-1);
Pre.tolocalestring ()//"2/28/2013 12:00:00 AM"

The code above can implement the functionality we need to get the right results, but it's not the best way for me.

When the third parameter we pass 0, represents the last day of one months, that is, the first day of the month, then the transmission is negative, or more than the maximum number of days of this month will be what? Let's look at the following code:

The code is as follows Copy Code

var d0 = new Date (2013,6,0),//2013-6-30
D1 = new Date (2013,6,-1),//2013-6-29
D2 = new Date (2013,6,-2),//2013-6-28

D3 = new Date (2013,6,32),//2013-8-1
D4 = new Date (2013,6,33); 2013-8-2

Visible, if beyond the normal range, will be postponed forward.

Besides, if we're not sure how many days we have in the last one months, we just want to add a few days to the end of the month, we have to set the time to do it:

The code is as follows Copy Code

var pre = new Date (2013,6,6);
Pre.tolocalestring ()//"7/6/2013 12:00:00 AM"
Pre.setdate (pre.getdate () + 30);
Pre.tolocalestring ()//"8/5/2013 12:00:00 AM"

Summary: The date in the development is still very common, previously did not notice the above some of the details of things, if you understand these details, may make the program more concise, simpler.

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.