published: 11 Sep 2022
2 min read
How to get the current day, month, and year in JavaScript
The Date
object methods getDate()
, getMonth()
, and getFullYear()
can be used to retrieve day, month, and full year from a date object in JavaScript.
Here is an example:
const date = new Date(2021, 8, 18);
const day = date.getDate();
const month = date.getMonth() + 1; // getMonth() returns month from 0 to 11
const year = date.getFullYear();
const str = '${day}/${month}/${year}';
console.log(str);
// 18/9/2021
Note that the getDate()
, getMonth()
, and getFullYear()
methods return the date and time in the local time zone of the computer where the code is running.
To get the date and time in the universal timezone (UTC), just replace the above methods with the getUTCDate()
, getUTCMonth()
, and getUTCFullYear()
respectively:
const date = new Date(2021, 8, 18);
const day = date.getUTCDate();
const month = date.getUTCMonth() + 1; // getUTCMonth() returns month from 0 to 11
const year = date.getUTCFullYear();
const str = '${day}/${month}/${year}';
console.log(str);
// 18/9/2021
Are we missing something? Help us improve this article. Reach out to us.
How to get the current day, month, and year in JavaScript
The Date
object methods getDate()
, getMonth()
, and getFullYear()
can be used to retrieve day, month, and full year from a date object in JavaScript.
Here is an example:
const date = new Date(2021, 8, 18);
const day = date.getDate();
const month = date.getMonth() + 1; // getMonth() returns month from 0 to 11
const year = date.getFullYear();
const str = '${day}/${month}/${year}';
console.log(str);
// 18/9/2021
Note that the getDate()
, getMonth()
, and getFullYear()
methods return the date and time in the local time zone of the computer where the code is running.
To get the date and time in the universal timezone (UTC), just replace the above methods with the getUTCDate()
, getUTCMonth()
, and getUTCFullYear()
respectively:
const date = new Date(2021, 8, 18);
const day = date.getUTCDate();
const month = date.getUTCMonth() + 1; // getUTCMonth() returns month from 0 to 11
const year = date.getUTCFullYear();
const str = '${day}/${month}/${year}';
console.log(str);
// 18/9/2021
Are you looking for other code tips?
JS Nooby
Javascript connoisseur