JavaScript Date Object

JavaScript Date Object

Describes the JavaScript Date Object including properties, constructors, and methods.

Properties

  • prototype - For creating more properties.

Constructors

  • Date() - Use the current date and time to create an instance of the object date.
  • Date(dateString) - Use the date specified by the string to create the instance of the date object. String format is "month day, year hours:minutes:seconds".
  • Date(year, month, day) - Create an instance of date with the specified values. Year is 0 to 99.
  • Date(year, month, day, hours, minutes, seconds) - Create an instance of date with the specified values.

Methods

  • getDate() - Get the day of the month. It is returned as a value between 1 and 31.
    var curdate = new Date()
    var mday = curdate.getDate()
    document.write(mday + "<BR>")
    

    The above code prints the day of the month.

  • getDay() - Get the day of the week as a value from 0 to 6
    var curdate = new Date()
    var wday = curdate.getDate()
    document.write(wday + "<BR>")
    

    The above code prints the day of the week.

  • getHours() - The value returned is 0 through 23.
    var curdate = new Date()
    var hours = curdate.getHours()
    document.write(hours + "<BR>")
    

    The above code prints the hours since midnight.

  • getMinutes() - The value returned is 0 through 59.
    var curdate = new Date()
    var minutes = curdate.getMinutes()
    document.write(minutes + "<BR>")
    

    The above code prints the minutes past the hour.

  • getMonth() - Returns the month from the date object as a value from 0 through 11.
    var curdate = new Date()
    var month = curdate.getMonth()
    document.write(month + "<BR>")
    

    The above code prints the numeric value of the month.

  • getSeconds() - The value returned is 0 through 59.
    var curdate = new Date()
    var seconds = curdate.getSeconds()
    document.write(seconds + "<BR>")
    

    The above code prints the seconds since the last minute.

  • getTime() - The number of milliseconds since January 1, 1970. this function allows you to manipulate the date object based on a millisecond value then convert it back to the form you want. In the example below, it is used to set a future expiration time of a cookie.
    var futdate = new Date()
    var expdate = futdate.getTime()
    expdate += 3600*1000 //expires in 1 hour(milliseconds) 
    futdate.setTime(expdate)
    
  • getTimeZoneOffset() - Time zone offset in hours which is the difference between GMT and local time.
    var curdate = new Date()
    var offset = curdate.getTimeZoneOffset()
    document.write(offset + "<BR>")
    

    The above code prints the number of hours different between your timezone and GMT. This value may change with daylight savings time..

  • getYear() - Returns the numeric four digit value of the year.
    var curdate = new Date()
    var year = curdate.getYear()
    document.write(year + "<BR>")
    

    The above code prints the numeric value of the year which is currently 2000.

  • parse() - The number of milliseconds after midnight January 1, 1970 till the given date espressed as a string in the example which is IETF format.
    var curdate = "Wed, 18 Oct 2000 13:00:00 EST"
    var dt = Date.parse(curdate)
    document.write(dt + "<BR>")
    
  • setDate(value) - Set the day of the month in the date object as a value from 1 to 31.
  • setHours(value) - Set the hours in the date object with a value of 0 through 59.
  • setMinutes(value) - Set the minutes in the date object with a value of 0 through 59.
  • setMonth(value) - Set the month in the date object as a value of 0 through 11.
  • setSeconds(value) - Set the seconds in the date object with a value of 0 through 59.
  • setTime(value) - Sets time on the basis of number of milliseconds since January 1, 1970. The below example sets the date object to one hour in the future.
    var futdate = new Date()
    var expdate = futdate.getTime()
    expdate += 3600*1000 //expires in 1 hour(milliseconds) 
    futdate.setTime(expdate)
    
  • setYear(value) - Set the year in the date instance as a 4 digit numeric value.
  • toGMTString() - Convert date to GMT format in a form similar to "Fri, 29 Sep 2000 06:23:54 GMT".
    var curdate = new Date()
    dstring = curdate.toGMTString()
    document.write(dstring + "<BR>" + curdate.toLocaleString() + "<BR>")
    

    The above example produces:

    Wed, 18 Oct 2000 18:08:11 UTC
    10/18/2000 14:08:11
    
  • toLocaleString() - Convert date to local time zone format. See the example, above.
  • UTC() - Based on a comma delimited string, the number of milliseconds after midnight January 1, 1970 GMT is returned. The syntax of the string is "year, month, day [, hrs] [, min] [, sec]". An example is "2000, 9, 29, 5, 43, 0" for Sept 29, 2000 at 5:43:0. The string is considered to be GMT. The hours, minutes, and seconds are optional.
    document.write(Date.UTC(2000, 9, 29, 5, 43, 0) + "
    ")

    The above example produces:

    972798180000