How to get the current date and time
Short answer
new Date() without arguments creates an object with the current date and time. toLocaleString() shows it in a readable form:
const now = new Date();
console.log(now.toLocaleString('en-GB'));Output:
03/10/2026, 10:13:15Separate parts
The getters return the year, month, day and time. Months are counted from zero:
const now = new Date();
console.log(now.getFullYear(), now.getMonth() + 1, now.getDate());
console.log(now.getHours(), now.getMinutes());Output:
2026 10 3
10 13A timestamp
Date.now() returns the number of milliseconds since January 1, 1970. It is convenient for measuring time and for unique values:
const start = Date.now();
console.log(typeof start, start > 0);Output:
number trueFor an API
toISOString() gives a standard string in UTC, which servers understand:
const now = new Date();
console.log(now.toISOString().length);Output:
24Common mistake
getDay() returns the day of the week, not the day of the month. The day of the month is getDate():
const date = new Date(2025, 2, 15);
console.log(date.getDay(), date.getDate());Output:
6 15Functions used in this recipe
- Date.now — Returns the current time as a number of milliseconds.
- Date.prototype.toLocaleString — Formats a date together with the time by language rules.
- Date.prototype.toISOString — Returns the date in ISO 8601 format in UTC.