Some usages of JavaScript date types Introduction _javascript Tips

Source: Internet
Author: User
Tags current time getdate

Get the number of days in a month

I believe that when you read primary school, you will know how many days a year 12 months, there is a special existence-February. The February leap year has 29 days, non leap year February is only 28 days. It is estimated that many people, like me, do not remember the rules of a leap year, and this is a useful method.

Copy Code code as follows:

var date = new Date (2013, 2, 0);
Date.getdate (); 28
Date = new Date (2012, 2, 0);
Date.getdate (); 29

When you create a Date object, you can pass in three parameters, namely, the year, month (0~11,0 January), day, and if the day argument is 0, the created object represents the last day of the last month, so you can tell how many days were there last month.

Similarly, we can use this method to determine whether a leap year is:

Copy Code code as follows:

function Isleapyear (year) {
return new Date (year, 2, 0). getDate () = = 29;
}
Isleapyear (2012); True

Get time zone

The getTimezoneOffset () method of the date type gets the time difference between Greenwich and local times, in minutes. For example:

Copy Code code as follows:

var date = new Date ();
var timezoneoffset = Date.gettimezoneoffset (); China (East Eight) is-480
-TIMEZONEOFFSET/60; 8

Divide the time difference you get by 60, and then the negative is where you are.

Besides, there is another way. After calling ToString () of the date type, you can get a fixed-format date string:

Copy Code code as follows:

New Date (). toString (); Sun Mar 2013 16:41:12 gmt+0800 (China Standard Time)

Obviously, the +800 in the back of GMT is the time zone we want, as long as the regular expression is matched to get the value.
Copy Code code as follows:

/GMT ([+-]\d+)/.test (New Date (). toString ());
var timezone = regexp.$1; +0800

But at this point the timezone variable is a string, and if you want to convert to a numeric type, you have to do some processing.

Calculate Run time

How do you measure the execution time of a program? The method is simple, record a time before execution, and then subtract the time before execution with the current time, and you get the result:

Copy Code code as follows:

var starttime = new Date ();
Some program
Console.log (New Date ()-starttime);

There is no need to manually convert a date to a number, because the conversion will naturally be cast when the subtraction is performed. The result is a millisecond, the precision is not very good, but for browser-side JavaScript, there is no need to tangle in 1 milliseconds of consumption.

Delete Cookies

To be exact, we can't delete cookies directly from JavaScript. The only way to erase a cookie from the world is to expire it, so that the built-in mechanism of the browser will automatically kill it.

The most straightforward way to get a cookie to expire is to set its expiration to a minimum value. The minimum date that can be represented in JavaScript is January 1, 1970 0 o'clock 0:0, and a Date object can be created with new date (0):

Copy Code code as follows:

var cookiename = ' name '; Cookie Name
Document.cookie = cookiename + ' = ' + '; Expires= ' + new Date (0). toUTCString ();

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.