Home > Web Front-end > JS Tutorial > Introduction to some usage of JavaScript date type_javascript skills

Introduction to some usage of JavaScript date type_javascript skills

WBOY
Release: 2016-05-16 16:11:45
Original
1160 people have browsed it

Get the number of days in a certain month

I believe that when you were in primary school, you all knew how many days there are in each of the twelve months of the year. There is a special existence in it - February. February in leap years has 29 days, while February in non-leap years has only 28 days. It is estimated that many people, like me, have forgotten the rules of leap years. At this time, the following method comes in handy.

Copy code 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 pass in three parameters, namely year, month (0~11, 0 means January), and day. If the day parameter is 0, the created object represents the last day of the previous month. , so you can know how many days there are in the last month.

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

Copy code 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 date type can get the time difference between Greenwich time and local time, in minutes. For example:

Copy code The code is as follows:

var date = new Date();
var timezoneOffset = date.getTimezoneOffset(); // China (East Eighth District) is -480
-timezoneOffset / 60; // 8

Divide the obtained time difference by 60, and then take the negative value to get the time zone.

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

Copy code The code is as follows:

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

Obviously, the 800 after GMT is the time zone we want. We can get the value by matching it with a regular expression.
Copy code The code is as follows:

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

However, the timezone variable at this time is a string. If it is to be converted into a numeric type, some processing needs to be done.

Calculate running time

How to measure the execution time of a certain program? The method is very simple. Record the time before execution. After execution, subtract the time before execution from the current time to get the result:

Copy code The code is as follows:

var startTime = new Date();
// some program
console.log(new Date() - startTime);

There is no need to manually convert the date to a number here, because the subtraction operation will naturally force the conversion. The result calculated in this way is at the millisecond level, which is not very accurate. However, for browser-side Javascript, there is no need to worry about consumption within 1 millisecond.

Delete cookies

To be precise, we cannot delete cookies directly through Javascript. The only way to erase a cookie from the world 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 smallest date that can be expressed in Javascript is 0:00:00 on January 1, 1970. You can create such a date object through new Date(0):

Copy code The code is as follows:

var cookieName = 'name'; // cookie name
document.cookie = cookieName '=' '; expires=' new Date(0).toUTCString();
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template