In daily life, various forms of time characters are everywhere. The emergence of the concept of time, the invention of time units and timing tools, has brought about changes to mankind. Let’s talk about dates today. Let's take a look at the date object Date in JavaScript.
Get the number of days in the month
// Get the number of days in the monthfunction getMonthDayCount(year, month) { return new Date(year, month, 0).getDate(); } (getMonthDayCount(2017, 10)); // 31
The essence of the third parameter of Date is the same as setDate.
Because the date is 0 automatically retreats to the last day of the previous month, the month here does not need to be reduced, which is just right.
Get the days in all months
function getAllMonthDayCount(year) { var days = [31, new Date(year, 2, 0).getDate(), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; return days; } (getAllMonthDayCount(2016));// [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
This is an extension of the above and will not explain much.
Is it a leap year?
function isLeapYear(year) { return (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0); }
This is the relevant code in the online web front-end development, and I believe most people use it. But actually, do you really understand or can you remember it? HTML and CSS alone are enough to remember
function isLeapYear(year) { return new Date(year, 2, 0).getDate() === 29; } ([ isLeapYear(2000), isLeapYear(2016), isLeapYear(2017), isLeapYear(2018) ]); // [ true, true, false, false ]
Looking at it this way, isn’t it very simple and easy to understand?
And it doesn’t need to be remembered. Can’t you forget it even if you want to?
Add or subtract the number of days
I have seen someone using relative seconds to calculate the situation of crossing the month and New Year's Eve in a few days or a few days later.
In fact, just setDate directly, automatically deal with the situation of cross-month and New Year's Eve.
// What month will be 10 days later
var dt = new Date('2016-12-25');
(() + 10);
(()); // 2017/1/4
// What month was it 10 days ago
var dt = new Date('2017-01-04');
(() - 10);
(()); // 2016/12/25
Below I will summarize the objects and methods of JavaScript Date objects into a table for reference, and you can also follow the JavaScript reference manual.
Date object properties
property | describe |
---|---|
constructor | Returns a reference to the Date function that created this object. |
prototype | Gives you the ability to add properties and methods to objects. |
Date object method
method | describe |
---|---|
Date() | Returns the date and time of the day. |
getDate() | fromDate objectReturn to a day of the month (1 ~ 31). |
getDay() | Returns a day of the week from the Date object (0 ~ 6). |
getMonth() | Returns month (0 ~ 11) from the Date object. |
getFullYear() | Returns the year as four digits from the Date object. |
getYear() | Please use the getFullYear() method instead. |
getHours() | Returns the hours (0 ~ 23) of the Date object. |
getMinutes() | Returns the minutes of the Date object (0 ~ 59). |
getSeconds() | Returns the number of seconds (0 ~ 59) of the Date object. |
etMilliseconds() | Returns milliseconds (0 ~ 999) of the Date object. |
getTime() | Returns the number of milliseconds from January 1, 1970 to present. |
getTimezoneOffset() | Returns the minute difference between local time and Greenwich Standard Time (GMT). |
getUTCDate() | Returns a day in the month from the Date object according to world time (1 ~ 31). |
getUTCDay() | Returns the day of the week from the Date object according to world time (0 ~ 6). |
getUTCMonth() | Returns month (0 ~ 11) from Date object according to world time. |
getUTCFullYear() | Returns a four-digit year from a Date object based on world time. |
getUTCHours() | Returns the hour (0 ~ 23) of the Date object according to world time. |
getUTCMinutes() | Returns the minutes of the Date object (0 ~ 59) based on world time. |
getUTCSeconds() | Returns the seconds (0 ~ 59) of the Date object according to the world time. |
getUTCMilliseconds() | Returns milliseconds of the Date object (0 ~ 999) based on world time. |
parse() | Returns the number of milliseconds from midnight on January 1, 1970 to the specified date (string). |
setDate() | Sets a day of the month in the Date object (1 ~ 31). |
setMonth() | Sets the month (0 ~ 11) in the Date object. |
setFullYear() | Sets the year (four digits) in the Date object. |
setYear() | Please use the setFullYear() method instead. |
setHours() | Sets the hours (0 ~ 23) in the Date object. |
setMinutes() | Sets the minutes (0 ~ 59) in the Date object. |
setSeconds() | Sets the seconds (0 ~ 59) in the Date object. |
setMilliseconds() | Sets milliseconds (0 ~ 999) in the Date object. |
setTime() | Sets the Date object in milliseconds. |
setUTCDate() | Sets the day of the month in the Date object according to world time (1 ~ 31). |
setUTCMonth() | Sets the month (0 ~ 11) in the Date object according to world time. |
setUTCFullYear() | Sets the year (four digits) in the Date object according to world time. |
setUTCHours() | Sets the hours (0 ~ 23) in the Date object according to world time. |
setUTCMinutes() | Sets minutes in the Date object according to world time (0 ~ 59). |
setUTCSeconds() | Sets the seconds (0 ~ 59) in the Date object according to world time. |
setUTCMilliseconds() | Set milliseconds (0 ~ 999) in the Date object according to world time. |
toSource() | Returns the source code of the object. |
toString() | Converts the Date object to a string. |
toTimeString() | Converts the time part of the Date object to a string. |
toDateString() | Converts the date part of the Date object to a string. |
toGMTString() | Please use the toUTCString() method instead. |
toUTCString() | Convert the Date object to a string based on the world time. |
toLocaleString() | Convert the Date object to a string according to the local time format. |
toLocaleTimeString() | Convert the time part of the Date object to a string according to the local time format. |
toLocaleDateString() | Convert the date part of the Date object to a string according to the local time format. |
UTC() | Returns the number of milliseconds from January 1, 1970 to the specified date according to the world time. |
valueOf() | Returns the original value of the Date object. |
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.