JavaScript Date: Get the Current Date, Time, and Weekday
Use new Date() to capture the current instant, then call methods such as getFullYear(), getMonth(), and getDate(). These return parts in the runtime’s local time zone. Add one to the month index for display, and use getDay() for the weekday.
Create the current or a fixed date
new Date() creates an object for the instant of the call. Called without new, Date() returns a string representing the current time. Numeric arguments make an example reproducible. The 7 below means August and the supplied clock time is interpreted in the runtime’s local zone. getTime() returns milliseconds since midnight on January 1, 1970 UTC. To refer to the same instant across environments, use a standard date-time string with Z or an explicit offset. ECMAScript · Date Constructor
const now = new Date();
const sample = new Date(2024, 7, 19, 14, 5, 9, 42);
console.log(now.getTime());
console.log(sample.getMonth()); // 7
Read the year, month, day, and weekday
getFullYear() returns the year, getMonth() a month index from 0 to 11, and getDate() a day of the month from 1 to 31. getDay() returns a weekday number from 0 for Sunday to 6 for Saturday. August 19, 2024 therefore gives 19 for the day of the month and 1 for Monday. ECMAScript · Date.prototype.getDay()
const date = new Date(2024, 7, 19);
console.log(date.getFullYear()); // 2024
console.log(date.getMonth() + 1); // 8
console.log(date.getDate()); // 19
console.log(date.getDay()); // 1
Format the time and milliseconds
getHours() returns 0–23, and getMinutes() and getSeconds() return 0–59. To display 5 as 05, convert it to a string and apply padStart(2, '0'). Pad the 0–999 result of getMilliseconds() to three places to display 42 as 042.
const date = new Date(2024, 7, 19, 14, 5, 9, 42);
const pad = (value, length = 2) => String(value).padStart(length, '0');
const hms = [date.getHours(), date.getMinutes(), date.getSeconds()];
const clock = hms.map(value => pad(value)).join(':');
console.log(clock + '.' + pad(date.getMilliseconds(), 3)); // 14:05:09.042
Try the complete date example
Save the following code as date-example.html and open it in a browser. It shows 2024-08-19, Monday, and 14:05:09.042. The weekday number indexes the array of names. The timestamp varies with the runtime’s time zone. To see the current values, replace the first line’s new Date(2024, 7, 19, 14, 5, 9, 42) with new Date() and refresh. The example computes the values once per load.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Explore the Date object</title>
</head>
<body>
<h1>Explore the Date object</h1>
<dl>
<dt>Date</dt>
<dd><output id="calendar"></output></dd>
<dt>Weekday</dt>
<dd><output id="weekday"></output></dd>
<dt>Time</dt>
<dd><output id="clock"></output></dd>
<dt>Timestamp</dt>
<dd><output id="timestamp"></output></dd>
</dl>
<script>
const date = new Date(2024, 7, 19, 14, 5, 9, 42);
const pad = (value, length = 2) => String(value).padStart(length, '0');
const weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
const calendar = [date.getFullYear(), pad(date.getMonth() + 1), pad(date.getDate())].join('-');
const hms = [date.getHours(), date.getMinutes(), date.getSeconds()];
const clock = hms.map(value => pad(value)).join(':') + '.' + pad(date.getMilliseconds(), 3);
document.querySelector('#calendar').textContent = calendar;
document.querySelector('#weekday').textContent = weekdays[date.getDay()];
document.querySelector('#clock').textContent = clock;
document.querySelector('#timestamp').textContent = String(date.getTime());
</script>
</body>
</html>
Distinguish local and UTC dates
toISOString() expresses an instant in UTC. Taking the date portion of that string yields the UTC date, which can differ from your local date around midnight. Use the earlier methods for the runtime’s local date and methods such as getUTCFullYear() for UTC date parts. For a named zone such as Seoul, pass timeZone to Intl.DateTimeFormat. The example below shows one instant when it is August 19 in UTC and already August 20 in Seoul. ECMAScript Internationalization API · DateTimeFormat
const instant = new Date('2024-08-19T23:30:00Z');
const seoul = new Intl.DateTimeFormat('en-GB', {
timeZone: 'Asia/Seoul',
year: 'numeric',
month: '2-digit',
day: '2-digit'
});
console.log(instant.toISOString().slice(0, 10)); // 2024-08-19
console.log(seoul.format(instant)); // 20/08/2024




