Javascript Date Objects and Method


Date Object

Date Methods

getDate Method
getDay Method
getFullYear Method
getHours Method
getMilliseconds Method
getMinutes Method
getMonth Method
getSeconds Method
getTime Method
getTimezoneOffset Method
getUTCDate Method
getUTCDay Method
getUTCFullYear Method
getUTCHours Method
getUTCMilliseconds Method
getUTCMinutes Method
getUTCMonth Method
getUTCSeconds Method
getVarDate Method
getYear Method
setDate Method
setFullYear Method
setHours Method
setMilliseconds Method
setMinutes Method
setMonth Method
setSeconds Method
setTime Method
setUTCDate Method
setUTCFullYear Method
setUTCHours Method
setUTCMilliseconds Method
setUTCMinutes Method
setUTCMonth Method
setUTCSeconds Method
setYear Method
toGMTString Method
toLocaleString Method
toUTCString Method

toString Method
valueOf Method

Nonmembers of Date.prototype

parse Method
UTC Method

Date Object

Description

Enables basic storage and retrieval of dates and times.

Syntax

var newDateObj = new Date()
var newDateObj = new Date(dateVal)
var newDateObj = new Date(year, month, date[, hours[, minutes[, seconds[,ms]]]])

The Date object constructor syntax has these parts:

Part Description
dateVal If a numeric value, dateVal represents the number of milliseconds in Universal Coordinated Time between the specified date and midnight January 1, 1970. If a string, dateVal is parsed according to the rules in the parse method. The dateVal argument can also be a VT_DATE value as returned from some ActiveX® objects.
year Required. The full year, for example, 1976 (and not 76).
month Required. The month as an integer between 0 and 11 (January to December).
date Required. The date as an integer between 1 and 31.
hours Optional. Must be supplied if minutes is supplied. An integer from 0 to 23 (midnight to 11pm) that specifies the hour.
minutes Optional. Must be supplied if seconds is supplied. An integer from 0 to 59 that specifies the minutes.
seconds Optional. Must be supplied if milliseconds is supplied. An integer from 0 to 59 that specifies the seconds.
ms Optional. An integer from 0 to 999 that specifies the milliseconds.
Remarks

A Date object contains a number representing a particular instant in time to within a millisecond. If the value of an argument is greater than its range or is a negative number, other stored values are modified accordingly. For example, if you specify 150 seconds, JScript redefines that number as two minutes and 30 seconds.

If the number is NaN, that indicates that the object does not represent a specific instant of time. If you pass no parameters to the Date object, it is initialized to the current time (UTC). A value must be given to the object before you can use it.

The range of dates that can be represented in a Date object is approximately 285,616 years on either side of January 1, 1970.

The Date object has two static methods that are called without creating a Date object. They are parse and UTC.


getFullYear Method

Description

Returns the year value in the Date object using local time.

Syntax

objDate.getFullYear()

Remarks

To get the year using Universal Coordinated Time (UTC), use the getUT CFullYear method.

The getFullYear method returns the year as an absolute number. For example, the year 1976 is returned as 1976. This avoids the classic year 2000 problem where dates beginning with January 1, 2000 are confused with those beginning with January 1, 1900.

The following example illustrates the use of the GetFullYear method:

function DateDemo()
{
  var d, s = "Today's UTC date is: ";
  d = new Date();
  s += (d.getMonth() + 1) + "/";
  s += d.getDate() + "/";
  s += d.getFullYear();
  return(s);
}

getDate Method
Description

Returns the day of the month value in a Date object using local time.

Syntax

objDate.getDate()

Remarks

To get the date value using Universal Coordinated Time (UTC), use the getUTCDate method.

The return value is an integer between 1 and 31 that represents the date value in the Date object.

The following example illustrates the use of the getDate method:

function DateDemo()
{
  var d, s = "Today's date is: ";
  d = new Date();
  s += (d.getMonth() + 1) + "/";
  s += d.getDate() + "/";
  s += d.getYear();
  return(s);
}

getDay Method

Description

Returns the day of the week value in a Date object using local time.

Syntax

objDate.getDay()

Remarks

To get the day using Universal Coordinated Time (UTC), use the getUTCDay method.

The value returned from the getDay method is an integer between 0 and 6 representing the day of the week and corresponds to a day of the week as follows:

0 = Sunday
1 = Monday
2 = Tuesday
3 = Wednesday
4 = Thursday
5 = Friday
6 = Saturday

The following example illustrates the use of the getDay method:

function DateDemo()
{
  var d, day, x, s = "Today is: ";
  var x = new Array("Sunday", "Monday", "Tuesday");
  var x = x.concat("Wednesday","Thursday", "Friday");
  var x = x.concat("Saturday");
  d = new Date();
  day = d.getDay();
  return(s += x[day]);
}

getHours Method

Description

Returns the hours value in a Date object using local time.

Syntax

objDate.getHours()

Remarks

To get the hours value using Universal Coordinated Time (UTC), use the getUTCHours method.

The getHours method returns an integer between 0 and 23 indicating the number of hours since midnight. A zero occurs in two situations: the time is before 1:00:00 am, or the time was not stored in the Date object when the object was created. The only way to determine which situation you have is to also check the minutes and seconds for zero values. If they are all zeroes, it is nearly certain that the time was not stored in the Date object.

The following example illustrates the use of the getHours method:

function TimeDemo()
{
  var d, s = "The current local time is: ";
  var c = ":";
  d = new Date();
  s += d.getHours() + c;
  s += d.getMinutes() + c;
  s += d.getSeconds() + c;
  s += d.getMilliseconds();
  return(s);
}

getMilliseconds Method

Description

Returns the milliseconds value in a Date object using local time.

Syntax

objDate.getMilliseconds()

Remarks

To get the number of milliseconds in Universal Coordinated Time (UTC), use the getUTCMilliseconds method.

The millisecond value returned can range from 0-999.

The following example illustrates the use of the getMilliseconds method:

function TimeDemo()
{
  var d, s = "The current local time is: ";
  var c = ":";
  d = new Date();
  s += d.getHours() + c;
  s += d.getMinutes() + c;
  s += d.getSeconds() + c;
  s += d.getMilliseconds();
  return(s);
}

getMinutes Method

Description

Returns the minutes value in a Date object using local time.

Syntax

objDate.getMinutes()

Remarks

To get the minutes value using Universal Coordinated Time (UTC), use the getUTCMinutes method.

The getMinutes method returns an integer between 0 and 59 equal to the minutes value stored in the Date object. A zero is returned in two situations: one occurs when the time is less than one minute after the hour. The other occurs when the time was not stored in the Date object when the object was created. The only way to determine which situation you have is to also check the hours and seconds for zero values. If they are all zeroes, it is nearly certain that the time was not stored in the Date object.

The following example illustrates the use of the getMinutes method:

function TimeDemo()
{
  var d, s = "The current local time is: ";
  var c = ":";
  d = new Date();
  s += d.getHours() + c;
  s += d.getMinutes() + c;
  s += d.getSeconds() + c;
  s += d.getMilliseconds();
  return(s);
}

getMonth Method

Description

Returns the month value in the Date object using local time.

Syntax

objDate.getMonth()

Remarks

To get the month value using Universal Coordinated Time (UTC), use the getUTCMonth method.

The getMonth method returns an integer between 0 and 11 indicating the month value in the Date object. The integer returned is not the traditional number used to indicate the month. It is one less. If "Jan 5, 1996 08:47:00" is stored in a Date object, getMonth returns 0.

The following example illustrates the use of the getMonth method:

function DateDemo()
{
  var d, s = "Today's date is: ";
  d = new Date();
  s += (d.getMonth() + 1) + "/";
  s += d.getDate() + "/";
  s += d.getYear();
  return(s);
}

getSeconds Method

Description

Returns the seconds value in a Date object using local time.

Syntax

objDate.getSeconds()

Remarks

To get the seconds value using Universal Coordinated Time (UTC), use the getUTCSeconds method.

The getSeconds method returns an integer between 0 and 59 indicating the seconds value of the indicated Date object. A zero is returned in two situations. One occurs when the time is less than one second into the current minute. The other occurs when the time was not stored in the Date object when the object was created. The only way to determine which situation you have is to also check the hours and minutes for zero values. If they are all zeroes, it is nearly certain that the time was not stored in the Date object.

The following example illustrates the use of the getSeconds method:

function TimeDemo()
{
  var d, s = "The current local time is: ";
  var c = ":";
  d = new Date();
  s += d.getHours() + c;
  s += d.getMinutes() + c;
  s += d.getSeconds() + c;
  s += d.getMilliseconds();
  return(s);
}

getTime Method

Description

Returns the time value in a Date object.

Syntax

objDate.getTime()

Remarks

The getTime method returns an integer value representing the number of milliseconds between midnight, January 1, 1970 and the time value in the Date object. The range of dates is approximately 285,616 years from either side of midnight, January 1, 1970. Negative numbers indicate dates prior to 1970.

When doing multiple date and time calculations, it is frequently useful to define variables equal to the number of milliseconds in a day, hour, or minute. For example:


var MinMilli = 1000 * 60
var HrMilli = MinMilli * 60
var DyMilli = HrMilli * 24

The following example illustrates the use of the getTime method:

function GetTimeTest()
{
  var d, s, t;
  var MinMilli = 1000 * 60;
  var HrMilli = MinMilli * 60;
  var DyMilli = HrMilli * 24;
  d = new Date();
  t = d.getTime();
  s = "It's been "
  s += Math.round(t / DyMilli) + " days since 1/1/70";
  return(s);
}

getTimezoneOffset Method

Description

Returns the difference in minutes between the time on the host computer and Universal Coordinated Time (UTC).

Syntax

objDate.getTimezoneOffset()

Remarks

The getTimezoneOffset method returns an integer value representing the number of minutes between the time on the current machine and UTC. These values are appropriate to the computer the script is executed on. If it is called from a server script, the return value is appropriate to the server. If it is called from a client script, the return value is appropriate to the client.

This number will be positive if you are behind UTC (e.g., Pacific Daylight Time), and negative if you are ahead of UTC (e.g., Japan).

For example, suppose a server in New York City is contacted by a client in Los Angeles on December 1. getTimezoneOffset returns 480 if executed on the client, or 300 if executed on the server.

The following example illustrates the use of the getTimezoneOffset method:

function TZDemo()
{
  var d, tz, s = "The current local time is ";
  d = new Date();
  tz = d.getTimezoneOffset();
  if (tz < 0)
    s += tz / 60 + " hours before GMT";
  else if (tz == 0)
    s += "GMT";
  else
    s += tz / 60 + " hours after GMT";
  return(s);
}

getUTCDate Method

Description

Returns the date in a Date object using Universal Coordinated Time (UTC).

Syntax

objDate.getUTCDate()

Remarks

To get the date using local time, use the getDate method.

The return value is an integer between 1 and 31 that represents the date value in the Date object.

The following example illustrates the use of the getUTCDate method:

function UTCDateDemo()
{
  var d, s = "Today's UTC date is: ";
  d = new Date();
  s += (d.getUTCMonth() + 1) + "/";
  s += d.getUTCDate() + "/";
  s += d.getUTCFullYear();
  return(s);
}

getUTCDay Method

Description

Returns the day of the week value in a Date object using Universal Coordinated Time (UTC).

Syntax

objDate.getUTCDay()

Remarks

To get the day of the week using local time, use the getDate method.

The value returned by the getUTCDay method is an integer between 0 and 6 representing the day of the week and corresponds to a day of the week as follows:

0 = Sunday
1 = Monday
2 = Tuesday
3 = Wednesday
4 = Thursday
5 = Friday
6 = Saturday

The following example illustrates the use of the getUTCDay method:

function DateDemo()
{
  var d, day, x, s = "Today is ";
  var x = new Array("Sunday", "Monday", "Tuesday");
  x = x.concat("Wednesday","Thursday", "Friday");
  x = x.concat("Saturday");
  d = new Date();
  day = d.getUTCDay();
  return(s += x[day]);
}

getUTCFullYear Method

Description

Returns the year value in a Date object using Universal Coordinated Time (UTC).

Syntax

objDate.getUTCFullYear()

Remarks

To get the year using local time, use the getFullYear method.

The getUTCFullYear method returns the year as an absolute number. This avoids the classic year 2000 problem where dates beginning with January 1, 2000 are confused with those beginning January 1, 1900.

The following example illustrates the use of the getUTCFullYear method:

function UTCDateDemo()
{
  var d, s = "Today's UTC date is: ";
  d = new Date();
  s += (d.getUTCMonth() + 1) + "/";
  s += d.getUTCDate() + "/";
  s += d.getUTCFullYear();
  return(s);
}

getUTCHours Method

Description

Returns the hours value in a Date object using Universal Coordinated Time (UTC).

Syntax

objDate.getUTCHours()

Remarks

To get the number of hours elapsed since midnight using local time, use the getHours method.

The getUTCHours method returns an integer between 0 and 23 indicating the number of hours since midnight. A zero occurs in two situations: the time is before 1:00:00 A.M., or a time was not stored in the Date object when the object was created. The only way to determine which situation you have is to also check the minutes and seconds for zero values. If they are all zeroes, it is nearly certain that the time was not stored in the Date object.

The following example illustrates the use of the getUTCHours method:

function UTCTimeDemo()
{
  var d, s = "Current Universal Coordinated Time (UTC) is: ";
  var c = ":";
  d = new Date();
  s += d.getUTCHours() + c;
  s += d.getUTCMinutes() + c;
  s += d.getUTCSeconds() + c;
  s += d.getUTCMilliseconds();
  return(s);
}


getUTCMilliseconds Method
Description

Returns the milliseconds value in a Date object using Universal Coordinated Time (UTC).

Syntax

objDate.getUTCMilliseconds()

Remarks

To get the number of milliseconds in local time, use the getMilliseconds method.

The millisecond value returned can range from 0-999.

The following example illustrates the use of the getUTCMilliseconds method:

function UTCTimeDemo()
{
  var d, s = "Current Universal Coordinated Time (UTC) is: ";
  var c = ":";
  d = new Date();
  s += d.getUTCHours() + c;
  s += d.getUTCMinutes() + c;
  s += d.getUTCSeconds() + c;
  s += d.getUTCMilliseconds();
  return(s);
}

getUTCMinutes Method

Description

Returns the minutes value in a Date object using Universal Coordinated Time (UTC).

Syntax

objDate.getUTCMinutes()

Remarks

To get the number of minutes stored using local time, use the getMinutes method.

The getUTCMinutes method returns an integer between 0 and 59 equal to the number of minutes value in the Date object. A zero occurs in two situations: the time is less than one minute after the hour, or a time was not stored in the Date object when the object was created. The only way to determine which situation you have is to also check the hours and seconds for zero values. If they are all zeroes, it is nearly certain that the time was not stored in the Date object.

The following example illustrates the use of the getUTCMinutes method:

function UTCTimeDemo()
{
  var d, s = "Current Universal Coordinated Time (UTC) is: ";
  var c = ":";
  d = new Date();
  s += d.getUTCHours() + c;
  s += d.getUTCMinutes() + c;
  s += d.getUTCSeconds() + c;
  s += d.getUTCMilliseconds();
  return(s);
}

getUTCMonth Method

Description

Returns the month value value in a Date object using Universal Coordinated Time (UTC).

Syntax

objDate.getUTCMonth()

Remarks

To get the month in local time, use the getMonth method.

The getUTCMonth method returns an integer between 0 and 11 indicating the month value in the Date object. The integer returned is not the traditional number used to indicate the month. It is one less. If "Jan 5, 1996 08:47:00.0" is stored in a Date object, getUTCMonth returns 0.

The following example illustrates the use of the getUTCMonth method:

function UTCDateDemo()
{
  var d, s = "Today's UTC date is: ";
  d = new Date();
  s += (d.getUTCMonth() + 1) + "/";
  s += d.getUTCDate() + "/";
  s += d.getUTCFullYear();
  return(s);
}

getUTCSeconds Method

Description

Returns the seconds value in a Date object using Universal Coordinated Time (UTC).

Syntax

objDate.getUTCSeconds()

Remarks

To get the number of seconds in local time, use the getSeconds method.

The getUTCSeconds method returns an integer between 0 and 59 indicating the seconds value of the indicated Date object. A zero occurs in two situations: the time is less than one second into the current minute, or a time was not stored in the Date object when the object was created. The only way to determine which situation you have is to also check the minutes and hours for zero values. If they are all zeroes, it is nearly certain that the time was not stored in the Date object.

The following example illustrates the use of the getUTCSeconds method:

function UTCTimeDemo()
{
  var d, s = "Current Universal Coordinated Time (UTC) is: ";
  var c = ":";
  d = new Date();
  s += d.getUTCHours() + c;
  s += d.getUTCMinutes() + c;
  s += d.getUTCSeconds() + c;
  s += d.getUTCMilliseconds();
  return(s);
}

getVarDate Method

Description

Returns the VT_DATE value in a Date object.

Syntax

dateobj.getVarDate( )

The dateobj argument is any Date object.

Remarks

The getVarDate method is used when interacting with ActiveX® objects or other objects that accept and return date values in VT_DATE format.


getYear Method

Description

Returns the year value in a Date object.

Syntax

objDate.getYear( )

Remarks

This method is obsolete, and is provided for backwards compatibility only. Use the getFullYear method instead.

For years from 1900 through 1999, the year is a 2-digit integer value returned as the difference between the stored year and 1900. For other dates, the 4-digit year is returned. For example, 1996 is returned as 96, but 1825 and 2025 are returned as-is.

Note For JScript version 1.0, getYear returns a value that is the result of the subtraction of 1900 from the year value in the provided Date object, regardless of the value of the year. For example, the year 1899 is returned as -1 and the year to 2000 is returned as 100.

The following example illustrates the use of the getYear method:

function DateDemo()
{
  var d, s = "Today's date is: ";
  d = new Date();
  s += (d.getMonth() + 1) + "/";
  s += d.getDate() + "/";
  s += d.getYear();
  return(s);
}

setDate Method

Description

Sets the numeric date of the Date object using local time.

Syntax

objDate.setDate(numDate)

The numDate argument is a numeric value equal to the numeric date.

Remarks

To set the date value using Universal Coordinated Time (UTC), use the setUTCDate method.

If the value of numDate is greater than the number of days in the month stored in the Date object or is a negative number, the date is set to a date equal to numDate minus the number of days in the stored month. For example, if the stored date is January 5, 1996, and setDate(32) is called, the date changes to February 1, 1996. Negative numbers have a similar behavior.

The following example illustrates the use of the setDate method:

function SetDateDemo(newdate)
{
  var d, s;
  d = new Date();
  d.setDate(newdate);
  s = "Current setting is ";
  s += d.toLocaleString(); 
  return(s);
}

setFullYear Method

Description

Sets the year value in the Date object using local time.

Syntax

objDate.setFullYear(numYear[, numMonth[, numDate]])

The setFullYear method syntax has these parts:

Part Description
numYear Required. A numeric value equal to the year.
numMonth Optional. A numeric value equal to the month. Must be supplied if numDate is supplied.
numDate Optional. A numeric value equal to the date.
Remarks

All set methods taking optional arguments use the value returned from corresponding get methods, if you do not specify an optional argument. For example, if the numMonth argument is optional, but not specified, JScript uses the value returned from the getMonth method.

In addition, if the value of an argument is greater than its range or is a negative number, other stored values are modified accordingly.

To set the year using Universal Coordinated Time (UTC), use the setUTCFullYear method.

The range of years supported in the date object is approximately 285,616 years from either side of 1970.

The following example illustrates the use of the setFullYear method:

function SetFullYearDemo(newyear)
{
  var d, s;
  d = new Date();
  d.setFullYear(newyear);
  s = "Current setting is ";
  s += d.toLocaleString(); 
  return(s);
}

setHours Method

Description

Sets the hour value in the Date object using local time.

Syntax

objDate.setHours(numHours[, numMin[, numSec[, numMilli]]])

The setHours method syntax has these parts:

Part Description
numHours Required. A numeric value equal to the hours value.
numMin Optional. A numeric value equal to the minutes value. Must be supplied if either of the following arguments are used.
numSec Optional. A numeric value equal to the seconds value. Must be supplied if the following argument is used.
numMilli Optional. A numeric value equal to the milliseconds value.
Remarks

All set methods taking optional arguments use the value returned from corresponding get methods, if you do not specify an optional argument. For example, if the numMonth argument is optional, but not specified, JScript uses the value returned from the getMonth method.

To set the hours value using Universal Coordinated Time (UTC), use the setUTCHours method.

If the value of an argument is greater than its range or is a negative number, other stored values are modified accordingly. For example, if the stored date is "Jan 5, 1996 00:00:00", and setHours(30) is called, the date is changed to "Jan 6, 1996 06:00:00." Negative numbers have a similar behavior.

The following example illustrates the use of the setHours method:

function SetHoursDemo(nhr, nmin, nsec)
{  
  var d, s;
  var sep = ":";
  d = new Date();
  d.setHours(nhr, nmin, nsec);
  s = "Current setting is " + d.toLocaleString() 
  return(s);
}

setMilliseconds Method

Description

Sets the milliseconds value in the Date object using local time.

Syntax

objDate.setMilliseconds(numMilli)

The numMilli argument is a numeric value equal to the millisecond value.

Remarks

All set methods taking optional arguments use the value returned from corresponding get methods, if you do not specify an optional argument. For example, if the numMonth argument is optional, but not specified, JScript uses the value returned from the getMonth method.

To set the milliseconds value using Universal Coordinated Time (UTC), use the setUTCMilliseconds method.

If the value of numMilli is greater than 999 or is a negative number, the stored number of seconds (and minutes, hours, and so forth if necessary) is incremented an appropriate amount.

The following example illustrates the use of the setMilliseconds method:

function SetMSecDemo(nmsec)
{  
  var d, s;
  var sep = ":";
  d = new Date();
  d.setMilliseconds(nmsec);
  s = "Current setting is ";
  s += d.toLocaleString() + sep + d.getMilliseconds();
  return(s);
}

setMinutes Method

Description

Sets the minutes value in the Date object using local time.

Syntax

objDate.setMinutes(numMinutes[, numSeconds[, numMilli]])

The setMinutes method syntax has these parts:

Part Description
numMinutes Required. A numeric value equal to the minutes value.
numSeconds Optional. A numeric value equal to the seconds value. Must be supplied if the numMilli argument is used.
numMilli Optional. A numeric value equal to the milliseconds value.
Remarks

All set methods taking optional arguments use the value returned from corresponding get methods, if you do not specify an optional argument. For example, if the numMonth argument is optional, but not specified, JScript uses the value returned from the getMonth method.

To set the minutes value using Universal Coordinated Time (UTC), use the setUTCMinutes method.

If the value of an argument is greater than its range or is a negative number, other stored values are modified accordingly. For example, if the stored date is "Jan 5, 1996 00:00:00" and setMinutes(90) is called, the date is changed to "Jan 5, 1996 01:30:00." Negative numbers have a similar behavior.

The following example illustrates the use of the setMinutes method:

function SetMinutesDemo(nmin, nsec)
{  
  var d, s;
  var sep = ":";
  d = new Date();
  d.setMinutes(nmin, nsec);
  s = "Current setting is " + d.toLocaleString() 
  return(s);
}

setMonth Method

Description

Sets the month value in the Date object using local time.

Syntax

objDate.setMonth(numMonth[, dateVal])

The setMonth method syntax has these parts:

Part Description
numMonth Required. A numeric value equal to the month.
dateVal Optional. A numeric value representing the date. If not supplied, the value from a call to the getDate method is used.
Remarks

To set the month value using Universal Coordinated Time (UTC), use the setUTCMonth method.

If the value of numMonth is greater than 11 (January is month 0) or is a negative number, the stored year is modified accordingly. For example, if the stored date is "Jan 5, 1996" and setMonth(14) is called, the date is changed to "Mar 5, 1997."

The following example illustrates the use of the setMonth method:

function SetMonthDemo(newmonth)
{  
  var d, s;
  d = new Date();
  d.setMonth(newmonth);
  s = "Current setting is ";
  s += d.toLocaleString(); 
  return(s);
}

setSeconds Method

Description

Sets the seconds value in the Date object using local time.

Syntax

objDate.setSeconds(numSeconds[, numMilli])

The setSeconds method syntax has these parts:

Part Description
numSeconds Required. A numeric value equal to the seconds value.
numMilli Optional. A numeric value equal to the milliseconds value.
Remarks

All set methods taking optional arguments use the value returned from corresponding get methods, if you do not specify an optional argument. For example, if the numMonth argument is optional, but not specified, JScript uses the value returned from the getMonth method.

To set the seconds value using Universal Coordinated Time (UTC), use the setUTCSeconds method.

If the value of an argument is greater than its range or is a negative number, other stored values are modified accordingly. For example, if the stored date is "Jan 5, 1996 00:00:00" and setSeconds(150) is called, the date is changed to "Jan 5, 1996 00:02:30."

The following example illustrates the use of the setSeconds method:

function SetSecondsDemo(nsec, nmsec)
{  
  var d, s;
  var sep = ":";
  d = new Date();
  d.setSeconds(nsec, nmsec);
  s = "Current setting is ";
  s += d.toLocaleString() + sep + d.getMilliseconds();
  return(s);
}

setTime Method

Description

Sets the date and time value in the Date object.

Syntax

objDate.setTime(milliseconds)

The milliseconds argument is an integer value representing the number of elapsed seconds since midnight, January 1, 1970 GMT.

Remarks

If milliseconds is negative, it indicates a date before 1970. The range of available dates is approximately 285,616 years from either side of 1970.

Setting the date and time with the setTime method is independent of the time zone.

The following example illustrates the use of the setTime method:

function SetTimeTest(newtime)
{
  var d, s;
  d = new Date();
  d.setTime(newtime);
  s = "Current setting is ";
  s += d.toUTCString();
  return(s);
}

setUTCDate Method

Description

Sets the numeric date in the Date object using Universal Coordinated Time (UTC).

Syntax

objDate.setUTCDate(numDate)

The numDate argument is a numeric value equal to the numeric date.

Remarks

To set the date using local time, use the setDate method.

If the value of numDate is greater than the number of days in the month stored in the Date object or is a negative number, the date is set to a date equal to numDate minus the number of days in the stored month. For example, if the stored date is January 5, 1996, and setUTCDate(32) is called, the date changes to February 1, 1996. Negative numbers have a similar behavior.

The following example illustrates the use of the setUTCDate method:

function SetUTCDateDemo(newdate)
{
  var d, s;
  d = new Date();
  d.setUTCDate(newdate);
  s = "Current setting is ";
  s += d.toUTCString(); 
  return(s);
}

setUTCFullYear Method

Description

Sets the year value in the Date object using Universal Coordinated Time (UTC).

Syntax

objDate.setUTCFullYear(numYear[, numMonth[, numDate]])

The setUTCFullYear method syntax has these parts:

Part Description
numYear Required. A numeric value equal to the year.
numMonth Optional. A numeric value equal to the month. Must be supplied if numDate is supplied.
numDate Optional. A numeric value equal to the date.
Remarks

All set methods taking optional arguments use the value returned from corresponding get methods, if you do not specify an optional argument. For example, if the numMonth argument is optional, but not specified, JScript uses the value returned from the getMonth method.

In addition, if the value of an argument is greater that its range or is a negative number, other stored values are modified accordingly.

To set the year using local time, use the setFullYear method.

The range of years supported in the Date object is approximately 285,616 years from either side of 1970.

The following example illustrates the use of the setUTCFullYear method:

function SetUTCFullYearDemo(newyear)
{
  var d, s;
  d = new Date();
  d.setUTCFullYear(newyear);
  s = "Current setting is ";
  s += d.toUTCString(); 
  return(s);
}

setUTCHours Method

Description

Sets the hours value in the Date object using Universal Coordinated Time (UTC).

Syntax

objDate.setUTCHours(numHours[, numMin[, numSec[, numMilli]]])

The setUTCHours method syntax has these parts:

Part Description
numHours Required. A numeric value equal to the hours value.
numMin Optional. A numeric value equal to the minutes value. Must be supplied if either numSec or numMilli are used.
numSec Optional. A numeric value equal to the seconds value. Must be supplied if numMilli argument is used.
numMilli Optional. A numeric value equal to the milliseconds value.
Remarks

All set methods taking optional arguments use the value returned from corresponding get methods, if you do not specify an optional argument. For example, if the numMonth argument is optional, but not specified, JScript uses the value returned from the getMonth method.

To set the hours value using local time, use the setHours method.

If the value of an argument is greater than its range or is a negative number, other stored values are modified accordingly. For example, if the stored date is "Jan 5, 1996 00:00:00.00", and setUTCHours(30) is called, the date is changed to "Jan 6, 1996 06:00:00.00."

The following example illustrates the use of the setUTCHours method:

function SetUTCHoursDemo(nhr, nmin, nsec)
{  
  var d, s;
  var sep = ":";
  d = new Date();
  d.setUTCHours(nhr, nmin, nsec);
  s = "Current setting is " + d.toUTCString() 
  return(s);
}

setUTCMilliseconds Method

Description

Sets the milliseconds value in the Date object using Universal Coordinated Time (UTC).

Syntax

objDate.setUTCMilliseconds(numMilli)

The numMilli argument is a numeric value equal to the milliseconds value.

Remarks

To set the milliseconds using local time, use the setMilliseconds method.

If the value of numMilli is greater than 999 or is a negative number, the stored number of seconds (and minutes, hours, and so forth, if necessary) is incremented an appropriate amount.

The following example illustrates the use of the setUTCMilliseconds method:

function SetUTCMSecDemo(nmsec)
{  
  var d, s;
  var sep = ":";
  d = new Date();
  d.setUTCMilliseconds(nmsec);
  s = "Current setting is ";
  s += d.toUTCString() + sep + d.getUTCMilliseconds();
  return(s);
}

setUTCMinutes Method

Description

Sets the minutes value in the Date object using Universal Coordinated Time (UTC).

Syntax

objDate.setUTCMinutes(numMinutes[, numSeconds[, numMilli]])

The setUTCMinutes method syntax has these parts:

Part Description
numMinutes Required. A numeric value equal to the minutes value.
numSeconds Optional. A numeric value equal to the seconds value. Must be supplied if numMilli is used.
numMilli Optional. A numeric value equal to the milliseconds value.
Remarks

All set methods taking optional arguments use the value returned from corresponding get methods, if you do not specify an optional argument. For example, if the numMonth argument is optional, but not specified, JScript uses the value returned from the getMonth method.

To modify the minutes value using local time, use the setMinutes method.

If the value of an argument is greater than its range or is a negative number, other stored values are modified accordingly. For example, if the stored date is "Jan 5, 1996 00:00:00.00", and setUTCMinutes(70) is called, the date is changed to "Jan 5, 1996 01:10:00.00."

The following example illustrates the use of the setUTCMinutes method:

function SetUTCMinutesDemo(nmin, nsec)
{  
  var d, s;
  var sep = ":";
  d = new Date();
  d.setUTCMinutes(nmin,nsec);
  s = "Current setting is " + d.toUTCString() 
  return(s);
}

setUTCMonth Method

Description

Sets the month value in the Date object using Universal Coordinated Time (UTC).

Syntax

objDate.setUTCMonth(numMonth[, dateVal])

The setUTCMonth method syntax has these parts:

Part Description
numMonth Required. A numeric value equal to the month.
dateVal Optional. A numeric value representing the date. If not supplied, the value from a call to the getUTCDate method is used.
Remarks

To set the month value using local time, use the setMonth method.

If the value of numMonth is greater than 11 (January is month 0) or is a negative number, the stored year is incremented or decremented appropriately. For example, if the stored date is "Jan 5, 1996 00:00:00.00", and setUTCMonth(14) is called, the date is changed to "Mar 5, 1997 00:00:00.00."

The following example illustrates the use of the setUTCMonth method:

function SetUTCMonthDemo(newmonth)
{  
  var d, s;
  d = new Date();
  d.setUTCMonth(newmonth);
  s = "Current setting is ";
  s += d.toUTCString(); 
  return(s);
}

setUTCSeconds Method

Description

Sets the seconds value in the Date object using Universal Coordinated Time (UTC).

Syntax

objDate.setUTCSeconds(numSeconds[, numMilli])

The setUTCSeconds method syntax has these parts:

Part Description
numSeconds Required. A numeric value equal to the seconds value.
numMilli Optional. A numeric value equal to the milliseconds value.
Remarks

All set methods taking optional arguments use the value returned from corresponding get methods, if you do not specify an optional argument. For example, if the numMonth argument is optional, but not specified, JScript uses the value returned from the getMonth method.

To set the seconds value using local time, use the setSeconds method.

If the value of an argument is greater than its range or is a negative number, other stored values are modified accordingly. For example, if the stored date is "Jan 5, 1996 00:00:00.00" and setSeconds(150) is called, the date is changed to "Jan 5, 1996 00:02:30.00."

The following example illustrates the use of the setSeconds method:

function SetUTCSecondsDemo(nsec, nmsec)
{  
  var d, s;
  var sep = ":";
  d = new Date();
  d.setUTCSeconds(nsec, nmsec);
  s = "Current setting is ";
  s += d.toUTCString() + sep + d.getUTCMilliseconds();
  return(s);
}

setYear Method

Description

Sets the year value in the Date object.

Syntax

objDate.setYear(numYear)

The numYear argument is a numeric value equal to the year minus 1900.

Remarks

This method is obsolete, and is maintained for backwards compatibility only. Use the setFullYear method instead.

To set the year of a Date object to 1997, call setYear(97). To set the year to 2010, call setYear(2010). Finally, to set the year to a year in the range 0-99, use the setFullYear method.

Note For JScript version 1.0, setYear uses a value that is the result of the addition of 1900 to the year value provided by the numYear, regardless of the value of the year. For example, to set the year to 1899 numYear is -1 and to set the year to 2000 numYear is 100.

The following example illustrates the use of the setYear method:

function SetYearDemo(newyear)
{  
  var d, s;
  d = new Date();
  d.setYear(newyear);
  s = "Current setting is ";
  s += d.toLocaleString(); 
  return(s);
}

toGMTString Method

Description

Returns a date converted to a string using Greenwich Mean Time(GMT).

Syntax

objDate.toGMTString()

Remarks

The toGMTString method is obsolete, and is provided for backwards compatibility only. It is recommended that you use the toUTCString method instead.

The toGMTString method returns a String object that contains the date formatted using GMT convention. The format of the return value is as follows: "05 Jan 1996 00:00:00 GMT."

The following example illustrates the use of the toGMTString method:

function toGMTStrDemo()
{  
  var d, s;
  d = new Date();
  s = "Current setting is ";
  s += d.toGMTString(); 
  return(s);
}

toLocaleString Method

Description

Returns a date converted to a string using the current locale.

Syntax

dateObj.toLocaleString( )

Remarks

The toLocaleString method returns a String object that contains the date written in the current locale's default format. The format of the return value depends on the current locale. For example, in the United States, toLocaleString may return "01/05/96 00:00:00" for January 5, but in Europe, it may return "05/01/96 00:00:00" for the same date, as European convention puts the day before the month.

The following example illustrates the use of the toLocaleString method:

function toLocaleStrDemo()
{  
  var d, s;
  d = new Date();
  s = "Current setting is ";
  s += d.toLocaleString(); 
  return(s);
}

toUTCString Method

Description

Returns a date converted to a string using Universal Coordinated Time (UTC).

Syntax

objDate.toUTCString()

Remarks

The toUTCString method returns a String object that contains the date formatted using UTC convention in a convenient, easily readable form.

The following example illustrates the use of the toUTCString method:

function toUTCStrDemo()
{  
  var d, s;
  d = new Date();
  s = "Current setting is ";
  s += d.toUTCString(); 
  return(s);
}

parse Method

Description

Parses a string containing a date, and returns the number of milliseconds between that date and midnight, January 1, 1970.

Syntax

Date.parse(dateVal)

The dateVal argument is either a string containing a date in a format such as "Jan 5, 1996 08:47:00" or a VT_DATE value retrieved from an ActiveX® object or other object.

Remarks

The parse method returns an integer value representing the number of milliseconds between midnight, January 1, 1970 and the date supplied in dateVal.

The parse method is a static method of the Date object. Because it is a static method, it is invoked as shown in the following example rather than invoked as a method of a created Date object.

var datestring = "November 1, 1997 10:15 AM";
Date.parse(datestring)

The following rules govern what the parse method can successfully parse:

  • Short dates can use either a "/" or "-" date separator, but must follow the month/day/year format, for example "7/20/96".
  • Long dates of the form "July 10 1995" can be given with the year, month, and day in any order, and the year in 2-digit or 4-digit form. If you use the 2-digit form, the year must be greater than or equal to 70.
  • Any text inside parentheses is treated as a comment. These parentheses may be nested.
  • Both commas and spaces are treated as delimiters. Multiple delimiters are permitted.
  • Month and day names must have two or more characters. Two character names that are not unique are resolved as the last match. For example, "Ju" is resolved as July, not June.
  • The stated day of the week is ignored if it is incorrect given the remainder of the supplied date. For example, "Tuesday November 9 1996" is accepted and parsed even though that date actually falls on a Friday. The resulting Date object contains "Friday November 9 1996".
  • JScript handles all standard time zones, as well as Universal Coordinated Time (UTC) and Greenwich Mean Time (GMT).
  • Hours, minutes, and seconds are separated by colons, although all need not be specified. "10:", "10:11", and "10:11:12" are all valid.
  • If the 24-hour clock is used, it is an error to specify "PM" for times later than 12 noon. For example, "23:15 PM" is an error.
  • A string containing an invalid date is an error. For example, a string containing two years or two months is an error.

The following example illustrates the use of the parse method:

function GetTimeTest(testdate)
{
  var d, s, t;
  var MinMilli = 1000 * 60;
  var HrMilli = MinMilli * 60;
  var DyMilli = HrMilli * 24;
  d = new Date();
  t = Date.parse(testdate);
  s = "There are "
  s += Math.round(Math.abs(t / DyMilli)) + " days "
  s += "between " + testdate + " and 1/1/70";
  return(s);
}

UTC Method

Description

Returns the number of milliseconds between midnight, January 1, 1970 Universal Coordinated Time (UTC) (or GMT) and the supplied date.

Syntax

Date.UTC(year, month, day[, hours[, minutes[, seconds[,ms]]]])

The UTC method syntax has these parts:

Part Description
year Required. The full year designation is required for cross-century date accuracy. If year is between 0 and 99 is used, then year is assumed to be 1900 + year.
month Required. The month as an integer between 0 and 11 (January to December).
date Required. The date as an integer between 1 and 31.
hours Optional. Must be supplied if minutes is supplied. An integer from 0 to 23 (midnight to 11pm) that specifies the hour.
minutes Optional. Must be supplied if seconds is supplied. An integer from 0 to 59 that specifies the minutes.
seconds Optional. Must be supplied if milliseconds is supplied. An integer from 0 to 59 that specifies the seconds.
ms Optional. An integer from 0 to 999 that specifies the milliseconds.
Remarks

The UTC method returns the number of milliseconds between midnight, January 1, 1970 UTC and the supplied date. This return value can be used in the setTime method and in the Date object constructor. If the value of an argument is greater than its range or is a negative number, other stored values are modified accordingly. For example, if you specify 150 seconds, JScript redefines that number as two minutes and 30 seconds.

The difference between the UTC method and the Date object constructor that accepts a date is that the UTC method assumes UTC, and the Date object constructor assumes local time.

The UTC method is a static method. Therefore, a Date object does not have to be created before it can be used. The UTC method is invoked as follows:

var datestring = "November 1, 1997 10:15 AM";
Date.UTC(datestring)

Note  If year is between 0 and 99, use 1900 + year for the year.

The following example illustrates the use of the UTC method:

function DaysBetweenDateAndNow(yr, mo, dy)
{
  var d, r, t1, t2, t3;
  var MinMilli = 1000 * 60
  var HrMilli = MinMilli * 60
  var DyMilli = HrMilli * 24
  t1 = Date.UTC(yr, mo, dy)
  d = new Date();
  t2 = d.getTime();
  if (t2 >= t1) 
    t3 = t2 - t1;
  else
    t3 = t1 - t2;
  r = Math.round(t3 / DyMilli);
  return(r);
}