DateTime Component Methods
Description
The following methods of the Date type deal directly with getting and setting specific parts of the date value.
- getTime() - Returns the milliseconds representation of the date; same as valueOf().
- setTime(milliseconds) - Sets time in the milliseconds.
- getFullYear() - Returns the four-digit year.
- getUTCFullYear() - Returns the four-digit year of the UTC date value.
- setFullYear(year) - Sets the year with four digits to the date.
- setUTCFullYear(year) - Sets the yea with fourdigitsr of the UTC date.
- getMonth() - Returns the month of the date, where 0 represents January and 11 represents December.
- getUTCMonth() - Returns the month of the UTC date, where 0 represents January and 11 represents December.
- setMonth(month) - Sets the month of the date, which is any number 0 or greater. Numbers greater than 11 add years.
- setUTCMonth(month) - Sets the month of the UTC date, which is any number 0 or greater. Numbers greater than 11 add years.
- getDate() - Returns the day of the month (1 through 31) for the date.
- getUTCDate() - Returns the day of the month (1 through 31) for the UTC date.
- setDate(date) - Sets the day of the month for the date. If the date is greater than the number of days in the month, the month value gets increased.
- setUTCDate(date) - Sets the day of the month for the UTC date. If the date is greater than the number of days in the month, the month value gets increased.
- getDay() - Returns the date's day of the week as a number (where 0 represents Sunday and 6 represents Saturday).
- getUTCDay() - Returns the UTC date's day of the week as a number (where 0 represents Sunday and 6 represents Saturday).
- getHours() - Returns the date's hours as a number between 0 and 23.
- getUTCHours() - Returns the UTC date's hours as a number between 0 and 23.
- setHours (hours) - Sets the date's hours. A number greater than 23 increments the day of the month.
- setUTCHours(hours) - Sets the UTC date's hours. A number greater than 23 increments the day of the month.
- getMinutes() - Returns the date's minutes as a number between 0 and 59.
- getUTCMinutes() - Returns the UTC date's minutes as a number between 0 and 59.
- setMinutes(minutes) - Sets the date's minutes. A number greater than 59 increments the hour.
- setUTCMinutes(minutes) - Sets the UTC date's minutes. A number greater than 59 also increments the hour.
- getSeconds() - Returns the date's seconds as a number between 0 and 59.
- getUTCSeconds() - Returns the UTC date's seconds as a number between 0 and 59.
- setSeconds(seconds) - Sets the date's seconds. A number greater than 59 also increments the minutes.
- setUTCSeconds(seconds) - Sets the UTC date's seconds. Value greater than 59 increments the minutes.
- getMilliseconds() - Returns the date's milliseconds.
- getUTCMilliseconds() - Returns the UTC date's milliseconds.
- setMilliseconds(milliseconds) - Sets the date's milliseconds.
- setUTCMilliseconds(milliseconds) - Sets the UTC date's milliseconds.
- getTimezoneOffset() - Returns the number of minutes that the local time zone is off set from UTC.
Example
var myDate = new Date();
myDate.setUTCFullYear(2012);/* w w w. j a va 2s . c o m*/
myDate.setFullYear(2012);
myDate.setUTCSeconds(12);
myDate.setUTCMinutes(21);
myDate.setMilliseconds(123);
myDate.setUTCMilliseconds(12);
myDate.setUTCHours(1);
myDate.setUTCDate(23);
myDate.setHours(3);
myDate.setMinutes(12);
myDate.setSeconds(21);
myDate.setUTCMonth(01);
myDate.setMonth(2);//the month value starting from 0
myDate.setTime(123);
myDate.setDate(12);
console.log(myDate);
console.log(myDate.getUTCHours());
console.log(myDate.getUTCMonth());
console.log(myDate.getMinutes());
console.log(myDate.getUTCFullYear());
console.log(myDate.getFullYear());
console.log(myDate.getHours());
console.log(myDate.getDay());
console.log(myDate.getMilliseconds());
console.log(myDate.getUTCDate());
console.log(myDate.getDate());
console.log(myDate.getSeconds());
console.log(myDate.getMonth());
console.log(myDate.getTime());
console.log(myDate.getUTCDay());
console.log(myDate.getTimezoneOffset());
The code above generates the following result.