Encodera

Thanks for reading!

Donate to the site — we work hard for you.

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:15

Separate 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 13

A 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 true

For an API

toISOString() gives a standard string in UTC, which servers understand:

const now = new Date();
 
console.log(now.toISOString().length);

Output:

24

Common 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 15

Functions used in this recipe

Thanks for reading!

Donate to the site — we work hard for you.