Introduction to some usage of the JavaScript date type _ javascript skills

Source: Internet
Author: User
This article describes how to use the JavaScript date type. This article describes how to obtain the number of days of a month, obtain the time zone, calculate the running time, and delete the cookie. For more information, see Returns the number of days of a month.

I believe that when I was in elementary school, I knew how many days each year and 12 months each had. There was a special existence here-January 1, February. A leap year has 29 days in February, and a non-leap year has only 28 days in February. It is estimated that many people, like me, do not remember the leap year rules. At this time, the following method will be used.

The Code is as follows:


Var date = new Date (2013, 2, 0 );
Date. getDate (); // 28
Date = new Date (2012, 2, 0 );
Date. getDate (); // 29


When creating a Date object, you can input three parameters: year and month (0 ~ 11,0 indicates August 1, January), and day. If the date parameter is 0, the created object indicates the last day of the previous month, so that you can know the number of days of the previous month.

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

The Code is 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 can obtain the time difference between Greenwich Mean Time and local time, in minutes. For example:

The Code is as follows:


Var date = new Date ();
Var timezoneOffset = date. getTimezoneOffset (); // China (UTC + 8) is-480
-TimezoneOffset/60; // 8


Divide the obtained time difference by 60, and then obtain the negative value as the time zone.

In addition, there is a method. After you call toString () of the date type, you can obtain a fixed date string:

The Code is as follows:


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


Obviously, the + 800 behind GMT is the time zone we need. You only need to match the value with a regular expression.

The Code is as follows:


/GMT ([+-] \ d +)/. test (new Date (). toString ());
Var timezone = RegExp. $1; // + 0800


However, the timezone variable is a string. If you want to convert it to a number type, you need to perform some processing.

Computing Run Time

How can we measure the execution time of a program? The method is simple. record the time before execution, and use the current time minus the time before execution to get the result:

The Code is as follows:


Var startTime = new Date ();
// Some program
Console. log (new Date ()-startTime );


Here, you do not need to manually convert the date to a number, because the subtraction operation will naturally force the conversion. The calculated result is millisecond-level, and the precision is not enough. However, for Javascript on the browser end, it is not necessary to worry about the consumption within 1 millisecond.

Delete cookie

To be precise, we cannot directly Delete cookies through Javascript. To erase a cookie from this world, the only way is to let it expire, so that the browser's built-in mechanism will automatically kill it.

The most straightforward way to make a cookie expire is to set its expiration time to the minimum value. The minimum Date that can be expressed in Javascript is 00:00, January 1, January 1, 1970. A Date object can be created through new Date (0:

The Code is 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.