Using Date( ) Object Methods

By Vince Barnes

Using the JavaScript Date( ) Object Methods - Continued.

Here we continue with discussion of the methods available in a JavaScript Date( ) Object (see the previous part of this series for the other available methods.)

While the previously discussed methods concerned themselves with getting and setting the elements of a date and time representation, the remaining elements are concerned with the full date and time representation, whether internal (milliseconds from Jan 1 1970) or external (human readable) representations, and the difference between UTC (GMT) and local time.  These are the remaining methods:

   
getTime( ) return the internal representation of the time
setTime( ) set the date and time using the millisecond format
   
valueOf( ) convert a date to the internal millisecond format
   
getTimezoneOffset( ) return the offset from GMT
   
toGMTstring( ) return the date and time as a string, using the GMT time zone
toLocaleString( ) return the date and time as a string, using the local time zone
toString( ) return the date and time as a string
toUTCString( ) return the date and time as a string, using Universal Time

Two other methods, getYear( ) and setYear( ), also exist but are deprecated in favor of the "Full" versions shown in the previous part of this series..

The constructor (remember part two?) also has two methods that you can invoke.  These are Date.parse( ) and Date.UTC( )

getTime( ) returns the internal representation of the time, which is the number of milliseconds between midnight at the beginning of January 1, 1970 and the time (date and time) held in the Date( ) object.  This is also a useful method for comparing two dates like this:

if firstdate.getTime( ) > seconddate.getTime( )
{ .........
 

setTime( ) is used to set the value of the Date( ) object.



Make a Comment

Loading Comments...

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
  •  
  •  
  •