I want to write a method for adding and subtracting dates by myself, but it involves the judgment of the number of days per month. If it is February, it also involves the judgment of leap years. It is a bit complicated and there are always problems during the application process. So I checked the information. To add and subtract the number of days on a certain date, in fact, just call the setDate() function of the Date object. The specific method is as follows:
function addDate(date,days){ var d=new Date(date); d.setDate(d.getDate()+days); var month=d.getMonth()+1; var day = d.getDate(); if(month<10){ month = "0"+month; } if(day<10){ day = "0"+day; } var val = d.getFullYear()+""+month+""+day; return val; }Among them, the date parameter is the date to be added and subtracted, the format is YYYY-MM-DD, and the days parameter is the number of days to be added and subtracted. If you calculate forward, you pass a negative number, and then you pass a positive number. If you want to add and subtract the month, you can just call setMonth() and getMonth(). It should be noted that the returned month is calculated from 0, which means that the returned month is one month less than the actual month, so 1 must be added accordingly.
In particular: Pay attention to the combination of year, month and day, it cannot be directly +. It will be summed as an int type and converted into a string.
PS: Finally, I will recommend several online tools related to time and date for your reference:
Online Date/Day Calculator:
http://tools.VeVB.COM/jisuanqi/date_jisuanqi
Online date calculator/phase difference day calculator:
http://tools.VeVB.COM/jisuanqi/datecalc
Online date day difference calculator:
http://tools.VeVB.COM/jisuanqi/onlinedatejsq
Unix timestamp conversion tool:
http://tools.VeVB.COM/code/unixtime