xxxxxxxxxx
// Get the current date and time
const currentDate = new Date();
console.log(currentDate);
// Get the current year
const currentYear = currentDate.getFullYear();
console.log(currentYear);
// Get the current month (0-indexed, so January is 0)
const currentMonth = currentDate.getMonth();
console.log(currentMonth);
// Get the current day of the month
const currentDay = currentDate.getDate();
console.log(currentDay);
// Get the current hour
const currentHour = currentDate.getHours();
console.log(currentHour);
// Get the current minutes
const currentMinutes = currentDate.getMinutes();
console.log(currentMinutes);
// Get the current seconds
const currentSeconds = currentDate.getSeconds();
console.log(currentSeconds);
xxxxxxxxxx
var dateWithTime = new Date().toLocaleString().replace(",", "")
console.log(dateWithTime)
//output :6/24/2022 9:36:33 PM
xxxxxxxxxx
// ⚠️ Edit :
// ⚠️ CHECK Intl : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl
//Do you need the current time ? ⌚
let date = new Date();
let time = ((date.getHours().toString()).length>1? date.getHours() : "0"+date.getHours()) +":"+ ((date.getMinutes().toString()).length>1? date.getMinutes() : "0"+date.getMinutes());
//If 4h-2min => 04:02
//If 20h-15min => 20:15
xxxxxxxxxx
// Get the current date and time
const currentDate = new Date();
// Get the current year, month, and day
const year = currentDate.getFullYear();
const month = currentDate.getMonth() + 1; // Months are zero-based, so add 1
const day = currentDate.getDate();
// Get the current hour, minute, and second
const hour = currentDate.getHours();
const minute = currentDate.getMinutes();
const second = currentDate.getSeconds();
// Display the date and time
console.log(`Current date and time: ${year}-${month}-${day} ${hour}:${minute}:${second}`);