Generate readable date function, with optional date diff


/ Published in: Other
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function adate(daysFromNow) {
  2. var date, datestr, mm, dd;
  3.  
  4. datestr = new Date().getTime();
  5.  
  6. if (!!daysFromNow) {
  7. datestr += (1000 * 60 * 60 * 24 * daysFromNow);
  8. }
  9.  
  10. date = new Date(datestr);
  11.  
  12. mm = date.getMonth() + '';
  13. if (mm.length == 1) {
  14. mm = '0' + mm;
  15. }
  16.  
  17. dd = date.getDate() + '';
  18. if (dd.length == 1) {
  19. dd = '0' + dd;
  20. }
  21.  
  22. return date.getFullYear() + '-' +
  23. mm + '-' + dd;
  24. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.