jquery - Javascript date offset -
i working on filter's page report. on page, have radio inputs user can select quick report options auto populate filters. there 1 field has stumped me. able to offset date field year, , month, when try offset day, follow:
seeing today 2/5/16, , try offset 7 days, calculate 2/29/16. counts correct number of days, fails go january , present 1/29/16. here javascript:
function datedayoffset(theday) { var = new date(); var m=now.getmonth()+1; now.setdate(now.getdate() + theday ); var d=now.getdate(); var y=now.getfullyear(); var dayoffset = m + "/" + d + "/" + y; return((zeropaddate(dayoffset))); };
and here using jquery in filters page:
$("#todate").val(datedayoffset(-7));
honestly, when comes dates, you're better off using moment.js handle stuff this. other issues such 28/29/30/31 day months affect if manually.
you can use moment().subtract(7, 'days')
accomplish want.
but regarding code, seems issue you're having you're getting month before change date. move code after line change date , should work fine.
function datedayoffset(theday) { var = new date(); now.setdate(now.getdate() + theday ); var m=now.getmonth()+1; var d=now.getdate(); var y=now.getfullyear(); var dayoffset = m + "/" + d + "/" + y; return((zeropaddate(dayoffset))); };
Comments
Post a Comment