Home

Current Date and Time in JavaScript

The new Date object always has the current date and time information of your PC. To create a new Date object, you can use:

const current = new Date();

There are multiple methods on Date.prototype, which you can use to get various details from the Date object.

const current = new Date();

console.log(current.toString()); // Complete day, date and time info
console.log(current.toDateString());
console.log(current.toTimeString());
console.log(current.getFullYear()); // Output → 2020
console.log(current.getDate());
console.log(current.getDay()); // A number representing day of the week
console.log(current.getHours());
console.log(current.getMilliseconds());
console.log(current.getMinutes());
console.log(current.getMonth()); // A number representing month of the year

For more info about date methods check getters and setters at MDN docs



Last Updated on

Next Post: Rules vs Guidelines →

Comments