Date Object Calculations

calendar September 29, 2009

Performing calculations with dates frequently requires working with the millisecond values of the Date objects, this is the surest way to compare date values.

Code:

<html>
 <head>
 <title>Date Calculations</title>
 <script type="text/javascript">
 function nWeek()
 {
   var todayMillisec = today.getTime();
   var nWeekMillisec = todayMillisec + (60*60*24*7*1000);
   var dayNextWeek = new Date(nWeekMillisec);
   return dayNextWeek;
 }
 </script>
 </head>
 <body>
 Today is:
 <script type="text/javascript">
 var today = new Date();
 document.write(today);
 </script>
 <br />
 Next week will be:
 <script type="text/javascript">
 document.write(nWeek());
 </script>
 </body>
</html>

In this example you could eliminate the function entirely and let the following two statements in the second body script obtain the desired result:

today.setDate(today.getDate() + 7);

document.write(today);

admin

Leave a Reply

You must be logged in to post a comment.