To get the current date and time in JavaScript, you can use the Date
object. Here's an example:
var currentDate = new Date();
console.log(currentDate);
This will output the current date and time in your browser's console.
If you want to display the date and time in a specific format, you can use various methods from the Date
object. Here are a few examples:
var currentDate = new Date();
// Get date in YYYY-MM-DD format
var date = currentDate.toISOString().slice(0, 10);
console.log(date);
// Get time in HH:MM:SS format
var time = currentDate.toTimeString().slice(0, 8);
console.log(time);
// Get full date and time in a custom format
var dateTime = currentDate.toLocaleString('en-US', { year: 'numeric', month: 'long', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric', hour12: true });
console.log(dateTime);
These examples demonstrate different ways to format the current date and time to meet your specific requirements.