let now = new Date();
console.log("Current date and time: " + now);
In this code, we create a new Date
object called now
, which automatically gets initialized to the current date and time. We then log this object to the console, which will display the date and time in the format Wed Jul 06 2022 15:26:18 GMT+0530 (India Standard Time)
, for example.
If you want to format the date and time in a specific way, you can use the toLocaleDateString()
and toLocaleTimeString()
methods, like this:
let now = new Date();
let date = now.toLocaleDateString();
let time = now.toLocaleTimeString();
console.log("Current date: " + date);
console.log("Current time: " + time);
In this code, toLocaleDateString()
returns the date in the format 7/6/2022
, and toLocaleTimeString()
returns the time in the format 15:26:18
, for example.
I hope this helps
Welcome to DevTechTutor.com, your ultimate resource for mastering web development and technology! Whether you're a beginner eager to dive into coding or an experienced developer looking to sharpen your skills, DevTechTutor.com is here to guide you every step of the way. Our mission is to make learning web development accessible, engaging, and effective.