/beyond/javascript/article.php/3470841/Using-Date--Object-Methods.htm Using Date( ) Object Methods

Using Date( ) Object Methods

By Vince Barnes

Having created an instance of a Date( ) object using the Date Constructor (see the previous part of this series) any of the Date( ) object methods can be used to manipulate the data in the object.  The methods available can be grouped by function as follows:

To get the various fields represented by the data contained in the Date( ) object:

getFullYear( ) return the four digit year
getMonth( ) return the month
getDate( ) return the day of the month
getDay( ) return the day of the week
getHours( ) return the hours
getMinutes( ) return the minutes
getSeconds( ) return the seconds
getMilliseconds( ) return the milliseconds

and to set those fields to a particular value:

setFullYear( ) set the four digit year
setMonth( ) set the month
setDate( ) set the day of the month
setDay( ) set the day of the week
setHours( ) set the hours
setMinutes( ) set the minutes
setSeconds( ) set the seconds
setMilliseconds( ) set the milliseconds

The use of these methods is fairly straightforward and is illustrated in the example below.  Take special note, however, of the get and set Day and Month.  Day is the day of the week expressed as an integer index value whose range, in typical JavaScript index value fashion, begins at zero.  Thus, zero is Sunday, one is Monday, etc. with 6 being Saturday.  Similarly, Month ranges from zero (January) to 11 (December).

The following two groups are the equivalent of the above, but where the time and date are expressed as Universal Time (ie GMT - see Timezone Offset in the next part of this series):

getUTCFullYear( ) return the Universal Time four digit year
getUTCMonth( ) return the Universal Time month
getUTCDate( ) return the Universal Time day of the month
getUTCDay( ) return the Universal Time day of the week
getUTCHours( ) return the Universal Time hours
getUTCMinutes( ) return the Universal Time minutes
getUTCSeconds( ) return the Universal Time seconds
getUTCMilliseconds( ) return the Universal Time milliseconds
setUTCFullYear( ) set the Universal Time four digit year
setUTCMonth( ) set the Universal Time month
setUTCDate( ) set the Universal Time day of the month
setUTCDay( ) set the Universal Time day of the week
setUTCHours( ) set the Universal Time hours
setUTCMinutes( ) set the Universal Time minutes
setUTCSeconds( ) set the Universal Time seconds
setUTCMilliseconds( ) set the Universal Time milliseconds

The following example determines how many days are left until a specific date -- in this case a birthday which occurs on December 15.  The Date( ) object constructor is used to create two instances of the Date( ) object; one for today and one for the birthday.  An "if" statement then checks to make sure the birthday hasn't already passed, and prints a message if not.  The the time difference is obtained by subtracting today's value from the birthday value and dividing the resulting millisecond value into days.  (The calculation uses Math.floor( ) which rounds a calculation down to the nearest integer value.)

<SCRIPT language="javascript">

today = new Date( );          // set today's date
birthday = new Date( );      // set up the Birthday object

birthday.setMonth(11);      // set birthday month to December
birthday.setDate(15);         // set birthday date to the 15th

if (today.getTime( ) < birthday.getTime( ))
 {  diff = birthday.getTime( ) - today.getTime( );
     diff = Math.floor(diff / (1000 * 60 * 60 * 24));
     document.write('There are ' + diff + ' days until December 15.');


As an interesting exercise, try enhancing this script to determine if today is the birthday, or if this year's birthday has already passed (there's already code for that) and printing an appropriate message in each case.


In the next part of this series, we continue with the use of the Date( ) object methods.


[Part Four:  Using The Date( ) Object Methods (contd.)]

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
Thanks for your registration, follow us on our social networks to keep up-to-date