JavaScript Introduction | Standard Objects | Date Methods
Date.now() Method
The Date.now() method returns the time from January 1, 1970 00:00:00 to the present as an integer in milliseconds.
var nowMiliSec = Date.now();
nowMiliSec; // milliseconds from January 1, 1970 00:00:00 to now
new Date(nowMiliSec); // returns the same result as new Date().
new Date();
Date.prototype getter Methods
Every Date instance inherits methods and properties from Date.prototype.
Date.prototype getter methods are methods for retrieving date-related information. The most commonly used representative getter methods are as follows.
- getFullYear()
- getDate()
- getDay()
- getTime()
getFullYear() Method
The getFullYear() method returns the current year as a four-digit number (YYYY).
var date = new Date();
document.write("This year is " + date.getFullYear() + "."); // returns the current year.
getDate() Method
The getDate() method returns the number corresponding to the current day of the month.
var date = new Date();
document.write("Today is " + date.getMonth() + "/" + date.getDate() + "."); // returns the current date.
getDay() Method
The getDay() method returns the number corresponding to the current day of the week.
In JavaScript, a week starts with Sunday (0) and ends with Saturday (6).
var date = new Date();
var day;
switch (date.getDay()) { // returns the current day of the week.
case 0:
day = "Sun";
break;
case 1:
day = "Mon";
break;
...
case 6:
day = "Sat";
break;
}
document.write("Today is " + day + ".");
Using an array lets you output the day of the week more simply.
var date = new Date();
var days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
document.write("Today is " + days[date.getDay()] + ".");
getTime() Method
The getTime() method returns the time from January 1, 1970 00:00:00 to the present converted to milliseconds as a number.
var date = new Date();
var period = date.getTime() / 86400000 // one day is calculated as 86,400,000 milliseconds.
document.write(period.toFixed() + " days have passed since January 1, 1970."); // omits the decimal part.
JavaScript Date.prototype getter Methods
| Method | Description | Value range |
|---|---|---|
| getDate() | Returns the number corresponding to the current day of the month in local time. | 1 ~ 31 |
| getDay() | Returns the number corresponding to the current day of the week in local time. | 0 ~ 6 |
| getMonth() | Returns the number corresponding to the current month in local time. | 0 ~ 11 |
| getFullYear() | Returns the current year in local time as a four-digit number (YYYY). | YYYY |
| getHours() | Returns the number corresponding to the current hour in local time. | 0 ~ 23 |
| getMilliseconds() | Returns the number corresponding to the milliseconds of the current time in local time. | 0 ~ 999 |
| getMinutes() | Returns the number corresponding to the minutes of the current time in local time. | 0 ~ 59 |
| getSeconds() | Returns the number corresponding to the seconds of the current time in local time. | 0 ~ 59 |
| getTime() | Returns the time from January 1, 1970 00:00:00 to the present converted to milliseconds as a number. | - |
| getTimezoneOffset() | Returns the time difference from UTC to the current time converted to minutes as a number. | - |
JavaScript Date.prototype UTC getter Methods
| Method | Description |
|---|---|
| getUTCDate() | Returns the number corresponding to the current day of the month in Coordinated Universal Time (UTC). |
| getUTCDay() | Returns the number corresponding to the current day of the week in UTC. |
| getUTCMonth() | Returns the number corresponding to the current month in UTC. |
| getUTCFullYear() | Returns the current year in UTC as a four-digit number (YYYY). |
| getUTCHours() | Returns the number corresponding to the current hour in UTC. |
| getUTCMilliseconds() | Returns the number corresponding to the milliseconds of the current time in UTC. |
| getUTCMinutes() | Returns the number corresponding to the minutes of the current time in UTC. |
| getUTCSeconds() | Returns the number corresponding to the seconds of the current time in UTC. |
Date.prototype setter Methods
Date.prototype setter methods are methods for setting date-related information. The most commonly used representative setter methods are as follows.
- setFullYear()
- setDate()
setFullYear() Method
The setFullYear() method sets the value of a Date object to a specific date.
var date = new Date();
date.setFullYear(1982, 1, 19); // February is 1 in JavaScript.
date.getFullYear(); // 1982
date.getMonth(); // 1
date.getDate(); // 19
setDate() Method
The setDate() method sets the day value of a Date object to a specific day.
var date = new Date();
date.setDate(10); // sets the day value of the Date object to the 10th.
document.write(date + "<br>");
date.setDate(40); // setting day 40 moves to the next month by the excess days.
document.write(date);
JavaScript Date.prototype setter Methods
| Method | Description | Value range |
|---|---|---|
| setDate() | Sets a specific day in local time. | 1 ~ 31 |
| setMonth() | Sets a specific month in local time. | 0 ~ 11 |
| setFullYear() | Sets a specific year in local time. It can also set month and day. | YYYY, MM, DD |
| setHours() | Sets a specific hour in local time. | 0 ~ 23 |
| setMilliseconds() | Sets a specific millisecond in local time. | 0 ~ 999 |
| setMinutes() | Sets a specific minute in local time. | 0 ~ 59 |
| setSeconds() | Sets a specific second in local time. | 0 ~ 59 |
| setTime() | Sets a specific time represented in milliseconds from January 1, 1970 00:00:00. | - |
There is no setDay() method in JavaScript.
JavaScript Date.prototype UTC setter Methods
| Method | Description | Value range |
|---|---|---|
| setUTCDate() | Sets a specific day in Coordinated Universal Time (UTC). | 1 ~ 31 |
| setUTCMonth() | Sets a specific month in UTC. | 0 ~ 11 |
| setUTCFullYear() | Sets a specific year in UTC. It can also set month and day. | YYYY, MM, DD |
| setUTCHours() | Sets a specific hour in UTC. | 0 ~ 23 |
| setUTCMilliseconds() | Sets a specific millisecond in UTC. | 0 ~ 999 |
| setUTCMinutes() | Sets a specific minute in UTC. | 0 ~ 59 |
| setUTCSeconds() | Sets a specific second in UTC. | 0 ~ 59 |