Date

Date Object Calculations

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);


Date Object

Date is a global object ready to be called upon at any time, the Date is  a static object. When you wish to work with a date, you must to invoke the  Date object constructor function to obtain an instance of a Date object. The value of a Date object is the time in milliseconds from zero o’clock on 1/1/ 1970. You can also grab a snapshot of the Date object for a particular date and time in the past or future by specifying that information as parameters to the Date object constructor function:

var dateObj = new Date(”Month dd, yyyy”);

Date object methods

var dateObj = new Date( );

  • method dateObj.getTime( )
  • value 0-…
  • description milliseconds since 1-1-1970 GTM

———————————————

  • method dateObj.getYear( )
  • value 70-…
  • description minus 1900;four-digit year for 2000+

———————————————

  • method dateObj.getFullYear( )
  • value 1970-…
  • description four-digit year

———————————————

  • method dateObj.getMonth( )
  • value 0-11
  • description month within the year

———————————————

  • method dateObj.getDate( )
  • value 1-31
  • description date within the month

———————————————

  • method dateObj.getDay( )
  • value 0-6
  • decsription day of week

———————————————

  • method dateObj.getHours( )
  • value 0-23
  • description hour of the day (24 hour time)

———————————————

  • method dateObj.getMinutes( )
  • value 0-59
  • description minute of the hour

———————————————

  • method dateObj.getSeconds( )
  • value 0-59
  • description second within a minute

———————————————

  • method dateObj.setTime( )
  • value 0-…
  • decsription milliseconds since 1-1-1970 GTM

———————————————

  • method dateObj.setYear( )
  • value 70-…
  • description minus 1900;four digit year for 2000+

———————————————

  • method dateObj.setMonth( )
  • value 0-11
  • description month within a year

———————————————

  • method dateObj.setDate( )
  • value 1-31
  • description date within the month

———————————————

  • method dateObj.setDay( )
  • value 0-6
  • description day of week

———————————————

  • method dateObj.setHours( )
  • value 0-23
  • description hour of the day (24 hour time)

———————————————

  • method dateObj.setMinutes( )
  • value 0-59
  • description minute of the hour

———————————————

  • method dateObj.setSeconds( )
  • value 0-59
  • description second within the minute

———————————————

Note: the getMonth( ) and setMonth( ) method values are zero based; January is 0, December is 11.