view · edit · attach · print · history

Lessons learned in JavaScript

Here is how to add and subtract dates in javascript:

 // Get Current Date
 var curDate = new Date();

 document.getElementById("Label1").innerHTML = curDate;

 // Get Date + 30 Days
 var newDate = new Date();
 var diffTime = curDate.getTime();
 diffTime += 30*24*3600*1000;
 newDate.setTime( diffTime );

 document.getElementById("Label2").innerHTML = newDate;

 // Get Date - 30 Days
 var diffTime = curDate.getTime();
 diffTime -= 30*24*3600*1000;
 newDate.setTime( diffTime );

 document.getElementById("Label3").innerHTML = newDate;

Next:

view · edit · attach · print · history
Page last modified on November 21, 2005, at 03:35 PM