Date Operations

ICE Data Services -

Date Operations

 

The following methods allow you to manipulate the system date.

 

Date Constructors

 

var newDateObject = new Date();

var newDateObject = new Date(dateValue);

var newDateObject = new Date(year, month, day, [hours], [minutes], [seconds], [milliseconds]);

var newDateObject = new Date(year, month, day);

var newDateObject = new Date(year month, day, hour, minutes, seconds);

 

If dateValue is a numeric value, it represents the number of milliseconds since January 1, 1970 00:00:00. The ranges of dates is approximately 285,616 years from either side of midnight. Negative numbers indicate dates prior to 1970.

 

Year: The full year (1980).

Month: 0-11 (January to December)

Day: 1-31

Hour: 0-23

Minutes: 0-59

Seconds: 0-59

Milliseconds: 0-999

 

Remarks

Once the date object is constructed, methods can be accessed by the following syntax:

 

var today = new Date();

 var h = today.getHours();

 var s = today.toString();

 

Local Time is defined as: the time on the computer from where the script is executed.

UTC (Universal Coordinated Time) refers to the time as set by the World Time Standard. Also known as GMT (Greenwich Mean Time).

 

Date Methods

 

getDate() - Returns the day of the month of the date object according to local time.

 

getDate() returns a value between 1 and 31.

var today = new Date();

var sDate = "The Date is:" + today.getMonth();

sDate += "/" + today.getDate();

sDate += "/" + today.getFullYear();

 

getDay() - Returns the day of the week of the date object according to local time.

 

getDay() returns a value between 0 and 6.

Return Values:

0 = Sunday

1 = Monday

2 = Tuesday

3 = Wednesday

4 = Thursday

5 = Friday

6= Saturday

var today = new Date();

var days = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");

var dayOfWeek = today.getDay();

var sDay = "Today is: " + days[dayOfWeek];

 

getFullYear() -Returns the year (with 1900 added in) of the date object.. (eg: 1980, 2001,�) according to local time.

 

var today = new Date();

var sDate = "The Date is:" + today.getMonth();

sDate += "/" + today.getDate();

sDate += "/" + today.getFullYear();

 

getHours() - Returns the hour of the date object according to local time.

 

getHours() returns a value between 0 and 23.

var today = new Date();

var sTime = "The Time is: " + today.getHours();

sTime += ":" + today.getMinutes();

sTime += ":" + today.getSeconds();

 

getMilliseconds() - Returns the milliseconds of the date object according to local time.

 

getMilliseconds() returns a value between 0 and 999.

var today = new Date();

var sTime = "The Time is: " + today.getHours();

sTime += ":" + today.getMinutes();

sTime += ":" + today.getSeconds();

sTime += "." + today.getMilliseconds();

 

getMinutes() - Returns the minutes of the date object according to local time.

 

getMinutes() returns a value between 0 and 59.

var today = new Date();

var sTime = "The Time is: " + today.getHours();

sTime += ":" + today.getMinutes();

sTime += ":" + today.getSeconds();

 

getMonth() - Returns the month of the date object according to local time.

 

getMonth() returns a value between 0 and 11. 0 being January. 11 being December.

var today = new Date();

var sDate = "The Date is:" + today.getMonth();

sDate += "/" + today.getDate();

sDate += "/" + today.getFullYear();

 

getSeconds() - Returns the seconds of the date object according to local time.

 

getSeconds() returns a value between 0 and 59.

var today = new Date();

var sTime = "The Time is: " + today.getHours();

sTime += ":" + today.getMinutes();

sTime += ":" + today.getSeconds();

 

getTime() - Returns the number of milliseconds since 1/1/1970 00:00:00 according to local time. Negative numbers indicate a date prior to 1/1/1970.

 

Units

1 second = 1000 milliseconds.

1 minute = 60 seconds * 1000 milliseconds = 60,000 ms

1 hour = 60 minutes * 60 seconds *1000 milliseconds = 3,600,000 ms

1 day = 24 hours * 60 minutes * 60 seconds * 1000 milliseconds = 86,400,000 ms

var today = new Date();

var sDays = Math.round(today.getTime() / 86400000) + " days elapsed since 1/1/1970";

 

getTimezoneOffset() - Returns the timezone offset between local time and GMT.

 

Returns the difference in minutes between the time on the local machine and UTC. 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.

 

getUTCDate() - Returns the day of the month of the date object according to UTC

 

getUTCDate() returns a value between 1 and 31.

var today = new Date();

var sDate = "The Date is:" + today.getUTCMonth();

sDate += "/" + today.getUTCDate();

sDate += "/" + today.getUTCFullYear();

 

getUTCDay() - Returns the day of the week of the date object according to UTC.

 

getUTCDay() returns a value between 0 and 6.

Return Values:

0 = Sunday

1 = Monday

2 = Tuesday

3 = Wednesday

4 = Thursday

5 = Friday

6= Saturday

var today = new Date();

var days = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");

var dayOfWeek = today.getUTCDay();

var sDay = "UTC Today is: " + days[dayOfWeek];

 

getUTCFullYear() -Returns the year (with 1900 added in) of the date object.. (eg: 1980, 2001,�) according to UTC

 

var today = new Date();

var sDate = "The Date is:" + today.getUTCMonth();

sDate += "/" + today.getUTCDate();

sDate += "/" + today.getUTCFullYear();

 

getUTCHours() - Returns the hour of the date object according to UTC.

 

getUTCHours() returns a value between 0 and 23.

var today = new Date();

var sTime = "The Time is: " + today.getUTCHours();

sTime += ":" + today.getUTCMinutes();

sTime += ":" + today.getUTCSeconds();

 

getUTCMilliseconds() - Returns the milliseconds of the date object according to UTC.

 

getUTCMilliseconds() returns a value between 0 and 999.

var today = new Date();

var sTime = "The Time is: " + today.getUTCHours();

sTime += ":" + today.getUTCMinutes();

sTime += ":" + today.getUTCSeconds();

sTime += "." + today.getUTCMilliseconds();

 

getUTCMinutes() - Returns the minutes of the date object according to UTC.

 

getUTCMinutes() returns a value between 0 and 59.

var today = new Date();

var sTime = "The Time is: " + today.getUTCHours();

sTime += ":" + today.getUTCMinutes();

sTime += ":" + today.getUTCSeconds();

 

getUTCMonth() - Returns the month of the date object according to UTC.

 

getUTCMonth() returns a value between 0 and 11. 0 being January. 11 being December.

var today = new Date();

var sDate = "The Date is:" + today.getUTCMonth();

sDate += "/" + today.getUTCDate();

sDate += "/" + today.getUTCFullYear();

 

getUTCSeconds() - Returns the seconds of the date object according to UTC.

 

getUTCSeconds() returns a value between 0 and 59.

var today = new Date();

var sTime = "The Time is: " + today.getUTCHours();

sTime += ":" + today.getUTCMinutes();

sTime += ":" + today.getUTCSeconds();

 

getYear() - Returns a two digit number representing the year of this date object.

 

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.

 

parse(dateString) - Returns the number of milliseconds in dateString since January 1, 1970, 00:00:00, local time.

 

The parse method takes dateString (such as "Nov 1, 2001", "11/1/2001", "Nov 1, 2001 09:30:00") and returns the number of milliseconds since January 1, 1970, 00:00:00 (local time). This function is useful for setting date values based on string values, for example in conjunction with the setTime() and the Date object.

Because parse is a static method of Date, you always use it as Date.parse(), rather than as a method of a Date object you created.

 

setDate(domValue) - Set the day of the month of this date object according to local time.

 

If the value of domValue 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 domValue minus the number of days in the stored month. For example, if the stored date is May 3, 2001, and setDate(32) is called, the date changes to June 1, 2001. Negative numbers have a similar behavior.

 

setFullYear(yearValue, [monthValue ] [ , [ domValue ] ] ) - Set the year of this date object according to local time.

 

yearValue - Required.

monthValue - Optional. Only required if domValue is supplied.

domValue - Optional.

All set methods that have optional arguments use the value already stored in the date. If you do not supply an optional argument (eg: monthValue), the month already stored in the date object will be used. Additionally, if the value of an argument is greater than its range or is a negative number, the other stored values are adjusted accordingly.

 

setHours(hourValue, [ minuteValue] [ , [ secondsValue ] [, [ millisecondsValue ] ] ] ) - Set the hour of this date object according to local time.

 

hourValue - Required.

minuteValue - Optional. Only required if secondsValue or millisecondsValue provided.

secondsValue - Optional. Only required if millisecondsValue provided.

millisecondsValue - Optional.

All set methods that have optional arguments use the value already stored in the date. If you do not supply an optional argument (eg: minuteValue), the minute already stored in the date object will be used. Additionally, if the value of an argument is greater than its range or is a negative number, the other stored values are modified accordingly.

If the value of a value parameter is larger than its range (eg: hours range from 0 to 23), other corresponding values are adjusted accordingly. If, for example, a stored date is: "11/16/2001 00:00:00", and setHours(36) is called, the date becomes "11/17/2001 12:00:00". Negative numbers behave in the opposite direction.

 

setMilliseconds(millisecondValue) - Set the milliseconds of this date object according to local time.

 

The range for millisecondValue is 0 through 999. If a number less than 0 or larger than 999 is provided, other field (eg: hours, minute, seconds) are adjusted accordingly.

 

setMinutes(minuteValue, [ secondsValue ] [ , [ millisecondsValue] ]) - Set the minutes of this date object according to local time.

 

minuteValue - Required.

secondsValue - Optional. Only required if millisecondsValue provided.

millisecondsValue - Optional.

All set methods that have optional arguments use the value already stored in the date. If you do not supply an optional argument (eg: secondsValue), the second already stored in the date object will be used. Additionally, if the value of an argument is greater than its range or is a negative number, the other stored values are modified accordingly.

If the value of a value parameter is larger than its range (eg: minutes range from 0 to 59), other corresponding values are adjusted accordingly. If, for example, a stored date is: "11/16/2001 00:00:00", and setMinutes(90) is called, the date becomes "11/16/2001 01:30:00". Negative numbers behave in the opposite direction.

 

setMonth(monthValue, [ domValue ] ) - Set the month of this date object according to local time.

 

monthValue - Required.

domValue - Optional.

All set methods that have optional arguments use the value already stored in the date. If you do not supply an optional argument (eg: domValue), the day of month already stored in the date object will be used. Additionally, if the value of an argument is greater than its range or is a negative number, the other stored values are adjusted accordingly.

If the value of a value parameter is larger than its range other corresponding values are adjusted accordingly. If, for example, a stored date is: "1/1/2001", and setMonth(15) is called, the date becomes "4/1/2001". Negative numbers behave in the opposite direction.

 

setSeconds(secondsValue, [ millisecondsValue ] ) - Set the seconds of this date object according to local time.

 

secondsValue - Required.

millisecondsValue - Optional.

All set methods that have optional arguments use the value already stored in the date. If you do not supply an optional argument (eg: millisecondsValue), the second already stored in the date object will be used. Additionally, if the value of an argument is greater than its range or is a negative number, the other stored values are modified accordingly.

If the value of a value parameter is larger than its range (eg: seconds range from 0 to 59), other corresponding values are adjusted accordingly. If, for example, a stored date is: "11/16/2001 00:00:00", and setSeconds(90) is called, the date becomes "11/16/2001 00:01:30". Negative numbers behave in the opposite direction.

 

setTime(millisecondsValue) - Set the number of milliseconds since 1970 according to local time.

 

The millisecondsValue argument is an integer value representing the number of elapsed seconds since midnight, January 1, 1970 GMT. If millisecondsValue is negative, it indicates a date before 1970. The range of available dates is approximately 285,616 years from either side of 1970.

 

setUTCDate(domValue) - Set the day of the month of this date object according to UTC.

 

If the value of domValue 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 domValue minus the number of days in the stored month. For example, if the stored date is May 3, 2001, and setUTCDate(32) is called, the date changes to June 1, 2001. Negative numbers have a similar behavior.

 

setUTCFullYear(yearValue, [monthValue ] [ , [ domValue ] ] ) - Set the year of this date object according to UTC.

 

yearValue - Required.

monthValue - Optional. Only required if domValue is supplied.

domValue - Optional.

All set methods that have optional arguments use the value already stored in the date. If you do not supply an optional argument (eg: monthValue), the month already stored in the date object will be used. Additionally, if the value of an argument is greater than its range or is a negative number, the other stored values are adjusted accordingly.

 

setUTCHours(hourValue, [ minuteValue] [ , [ secondsValue ] [, [ millisecondsValue ] ] ] ) - Set the hour of this date object according to UTC.

 

hourValue - Required.

minuteValue - Optional. Only required if secondsValue or millisecondsValue provided.

secondsValue - Optional. Only required if millisecondsValue provided.

millisecondsValue - Optional.

All set methods that have optional arguments use the value already stored in the date. If you do not supply an optional argument (eg: minuteValue), the minute already stored in the date object will be used. Additionally, if the value of an argument is greater than its range or is a negative number, the other stored values are modified accordingly.

If the value of a value parameter is larger than its range (eg: hours range from 0 to 23), other corresponding values are adjusted accordingly. If, for example, a stored date is: "11/16/2001 00:00:00", and setUTCHours(36) is called, the date becomes "11/17/2001 12:00:00". Negative numbers behave in the opposite direction.

 

setUTCMilliseconds(millisecondsValue) - Set the milliseconds of this date object according to UTC.

 

The range for millisecondValue is 0 through 999. If a number less than 0 or larger than 999 is provided, other field (eg: hours, minute, seconds) are adjusted accordingly.

 

setUTCMinutes(minuteValue, [ secondsValue ] [ , [ millisecondsValue] ] ) - Set the minutes of this date object according to UTC.

 

minuteValue - Required.

secondsValue - Optional. Only required if millisecondsValue provided.

millisecondsValue - Optional.

All set methods that have optional arguments use the value already stored in the date. If you do not supply an optional argument (eg: secondsValue), the second already stored in the date object will be used. Additionally, if the value of an argument is greater than its range or is a negative number, the other stored values are modified accordingly.

If the value of a value parameter is larger than its range (eg: minutes range from 0 to 59), other corresponding values are adjusted accordingly. If, for example, a stored date is: "11/16/2001 00:00:00", and setUTCMinutes(90) is called, the date becomes "11/16/2001 01:30:00". Negative numbers behave in the opposite direction.

 

setUTCMonth(monthValue, [ domValue ] ) - Set the month of this date object according to UTC.

 

monthValue - Required.

domValue - Optional.

All set methods that have optional arguments use the value already stored in the date. If you do not supply an optional argument (eg: domValue), the day of month already stored in the date object will be used. Additionally, if the value of an argument is greater than its range or is a negative number, the other stored values are adjusted accordingly.

If the value of a value parameter is larger than its range other corresponding values are adjusted accordingly. If, for example, a stored date is: "1/1/2001", and setUTCMonth(15) is called, the date becomes "4/1/2001". Negative numbers behave in the opposite direction.

 

setUTCSeconds(secondsValue, [ millisecondsValue ] ) - Set the seconds of this date object according to UTC.

 

secondsValue - Required.

millisecondsValue - Optional.

All set methods that have optional arguments use the value already stored in the date. If you do not supply an optional argument (eg: millisecondsValue), the second already stored in the date object will be used. Additionally, if the value of an argument is greater than its range or is a negative number, the other stored values are modified accordingly.

If the value of a value parameter is larger than its range (eg: seconds range from 0 to 59), other corresponding values are adjusted accordingly. If, for example, a stored date is: "11/16/2001 00:00:00", and setUTCSeconds(90) is called, the date becomes "11/16/2001 00:01:30". Negative numbers behave in the opposite direction.

 

toDateString()

 

Returns a string in the format "Sun Jan 13 2002"

 

toLocaleDateString() - Converts a date to a string using the current locale.

(e.g.) Returns a string in the format "Sunday, January 13, 2002"

 

toLocaleTimeString() Converts a time to a string using the current locale.

(e.g.) Returns a string in the format "08:57:04"

 

toString() /* Tue Oct 31 09:41:40 GMT-0800 (PST) 2000 */

Returns a string in the format:

"Sun Jan 13 08:57:04 GMT-0800 (Pacific Standard Time) 2002"

 

toTimeString()

Returns a string in the format:

"08:57:04 GMT-0800 (Pacific Standard Time)"

 

toUTCString()

Returns a string in the format:

"Sun, 13 Jan 2002 16:57:04 GMT"

 

UTC(yearValue, monthValue, domValue [, hourValue [, monthValue [, secondsValue [, msValue ]]]]) - Returns the number of milliseconds since 1/1/1970 00:00:00 in a specified date.

 

UTC returns the number of milliseconds between the supplied date and 1/1/1970 00:00:00 (UTC). Dates before 1/1/1970 are represented as a negative number.

UTC is a static method of the Date class. It does not require a date object being constructed before it is used.