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.
<!DOCTYPE html>
<html>
<body>
<script>
var d = new Date('2017-7-16');
d.setHours(2);
d.setMinutes(100);
console.log(d.getFullYear());
console.log(d.getMonth());
console.log(d.getDate());
console.log(d.getHours());
console.log(d.getMinutes());
console.log(d.getTime());
console.log(d.getDay()); // Sunday
console.log(d.getUTCDay()); // Saturday
console.log(d.getTimezoneOffset()); // GMT+8
d.setDate(d.getDate() + 1); // to the next day
</script>
</body>
</html>2017
6
16
3
40
1500147600000
0
6
-480
| 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} |