Posted By

crevier on 12/14/09


Tagged

date days add


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

bullzito


date math


 / Published in: JavaScript
 

This function accepts a date string (yyyymmdd), along with a number of days (integer) and adds the days to the date. It then returns the new date string (yyyymmdd).

  1. function addToDate(date,days) {
  2. var yyyy,mm,dd,idate,newdate;
  3.  
  4. date = String(date);
  5. yyyy = date.substr(0,4);
  6. mm = date.substr(4,2) - 1;
  7. dd = date.substr(6,2) - 0;
  8. idate = new Date(yyyy,mm,dd,0,0,0,0);
  9.  
  10. idate.setTime(idate.getTime()+60000*60*24*days);
  11.  
  12. yyyy = idate.getYear(); if (yyyy < 1000) yyyy += 1900;
  13. mm = idate.getMonth() + 1; if (mm < 10) mm = '0' + mm;
  14. dd = idate.getDate(); if (dd < 10) dd = '0' + dd;
  15.  
  16. newdate = parseInt(String(yyyy) + String(mm) + String(dd));
  17.  
  18. return newdate;
  19. }

Report this snippet  

You need to login to post a comment.