Calculate how many days before the specific time. For example today is September 5, that 7 days ago Normal is August 29.
Before the direct use of time to add and subtract, eat a big loss, and then brain residual to directly write a very complicated calculation leap year, leap month, 30, 31, 28 months now share.
function Initdatetime (day) { var nowdate = new Date () var agodate = new Date (nowdate); var nowday = Nowdate.getdate (); var day =-day + nowday; Agodate.setdate (day); var nowmonth = nowdate.getmonth () + 1; var nowyear = Nowdate.getfullyear (); var agoday = Agodate.getdate (); var agomonth = agodate.getmonth () + 1; var agoyear = Agodate.getfullyear (); var datefrom = (agoyear.tostring () + '-' + agomonth.tostring () + '-' + agoday.tostring ()); var Dateto = (nowyear.tostring () + '-' + nowmonth.tostring () + '-' + nowday); $ (': text[name=startdate] '). Val (datefrom); Start time $ (': text[name=enddate] '). Val (Dateto); End Time }
Call:
Initdatetime (7) //calculates the time 7 days ago.
JS How to set the time to calculate a few days ago?