Get the number of days in a certain month
I believe that when you were in elementary school, you would know how many days each year and twelve months are there. There is a special existence here - February. February in a leap year has 29 days, and February in a non-leap year has only 28 days. I guess many people, like me, no longer remember the rules of leap years. At this time, the following method came in handy.
The code copy 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 represents January), and day. If the parameter of day is 0, the created object represents the last day of the last month, so you can know how many days there were last month.
Similarly, we can also use this method to determine whether a certain year is a leap year:
The code copy is as follows:
function isLeapYear(year) {
return new Date(year, 2, 0).getDate() === 29;
}
isLeapYear(2012); // true
Get the time zone
The getTimezoneOffset() method of date type gets the time difference between Greenwich time and local time in minutes. For example:
The code copy 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 be the time zone.
In addition, there is another method. After calling toString() of date type, you can get a date string in a fixed format:
The code copy is as follows:
new Date().toString(); // Sun Mar 10 2013 16:41:12 GMT+0800 (China Standard Time)
Obviously, +800 after GMT is the time zone we want. You can get the value by matching it through a regular expression.
The code copy 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 you want to convert it to a numeric type, you need to do some processing.
Calculate the run time
How to measure the execution time of a certain program? The method is very simple. Record the time before execution, and subtract the time before execution by using the current time to get the result:
The code copy is as follows:
var startTime = new Date();
// some program
console.log(new Date() - startTime);
There is no need to convert dates to numbers manually here, because the conversion will naturally be forced when performing subtraction operations. The result of this calculation is milliseconds, and the accuracy is not very enough. However, for Javascript on the browser side, there is no need to worry about consumption within 1 millisecond.
Delete cookies
To be precise, we cannot delete cookies directly through Javascript. If you want to erase a cookie from this world, the only way is to make 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 a minimum. The minimum date that can be represented in Javascript is 0:00 on January 1, 1970. Through new Date(0) you can create such a date object:
The code copy is as follows:
var cookieName = 'name'; // cookie name
document.cookie = cookieName + '=' + '; expires=' + new Date(0).toUTCString();