MENU
Prototype Setters and Getters
Many Date prototype methods can be summarized by the form:
Date.prototype. {get|set}[UTC]{FullYear | Month | Date | Hours |
Minutes | Seconds | Milliseconds}() | {get | set}Time() | get{[UTC]Day | TimezoneOffset}()
The optional [UTC] tag specifies a reference to the Coordinated Universal Time instead of the local time.'Time' refers to the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC. The prefix + may also be used to achieve the same purpose.
'Day' specifies the day number of the week (0-6).'TimezoneOffset' is the offset from Greenwich Mean Time in minutes.
RESETRUNFULL
<!DOCTYPE html><html><body><script>
<!DOCTYPE html><html><body><script>
var d = new Date('2017-7-16');
d.setHours(2);
d.setMinutes(100);
console.log(d.getFullYear()); // 2017
console.log(d.getMonth()); // 6
console.log(d.getDate()); // 18
console.log(d.getHours()); // 3
console.log(d.getMinutes()); // 40
console.log(d.getTime()); // 1500320400000
console.log(d.getDay()); // 0 (Sunday)
console.log(d.getUTCDay()); // 6 (Saturday)
console.log(d.getTimezoneOffset()); // -480 (GMT+8)
d.setDate( d.getDate() + 1 ); // to the next day</script></body></html>
</script></body><html>
The following shows how to check if something is a valid date. |
if ( Object.prototype.toString.call(d) === "[object Date]" ) { // it is a date if ( isNaN( d.getTime() ) ) { // d.valueOf() could also work // date is not valid } else { // date is valid }}else { // not a date} |