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}`);
xxxxxxxxxx
var d= new Date();
d.getFullYear();//Get the year as a four digit number (yyyy)
d.getMonth();//Get the month as a number (0-11)
d.getDate();//Get the day as a number (1-31)
d.getHours();//Get the hour (0-23)
d.getMinutes();//Get the minute (0-59)
d.getSeconds();//Get the second (0-59)
d.getMilliseconds();//Get the millisecond (0-999)
d.getTime();//Get the time (milliseconds since January 1, 1970)
xxxxxxxxxx
var d= new Date();
d.getFullYear();//Get the year as a four digit number (yyyy)
d.getMonth();//Get the month as a number (0-11)
d.getDate();//Get the day as a number (1-31)
d.getHours();//Get the hour (0-23)
d.getMinutes();//Get the minute (0-59)
d.getSeconds();//Get the second (0-59)
d.getMilliseconds();//Get the millisecond (0-999)
d.getTime();//Get the time (milliseconds since January 1, 1970)
xxxxxxxxxx
var dateWithTime = new Date().toLocaleString().replace(",", "")
console.log(dateWithTime)
//output :6/24/2022 9:36:33 PM
xxxxxxxxxx
var currentDate = new Date();
var dateFromTimeStamp= new Date(1560807962);
var dateFromString = new Date('December 17, 1995 03:24:00');
//new Date(year, month, day, hours, minutes, seconds, milliseconds)
//0= Jan, 11=Dec
var dateFromYearMonthDay = new Date(2018, 11, 26, 0, 0, 0, 0);
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
const today = new Date();
const yyyy = today.getFullYear();
let mm = today.getMonth() + 1; // Months start at 0!
let dd = today.getDate();
if (dd < 10) dd = '0' + dd;
if (mm < 10) mm = '0' + mm;
const formattedToday = dd + '/' + mm + '/' + yyyy;
document.getElementById('DATE').value = formattedToday;
xxxxxxxxxx
var d = new Date();
//1628202691394 miliseconds passed since 1970
Number(d)
Date("2017-06-23"); // date declaration
Date("2017"); // is set to Jan 01
Date("2017-06-23T12:00:00-09:45"); // date - time YYYY-MM-DDTHH:MM:SSZ
Date("June 23 2017"); // long date format
Date("Jun 23 2017 07:45:00 GMT+0100 (Tokyo Time)"); // time zone
xxxxxxxxxx
<!DOCTYPE html>
<html>
<body>
<button type="button"
onclick="document.getElementById('display').innerHTML = Date()">
Click me to display Date and Time.</button>
<p id="display"></p>
</body>
</html>
xxxxxxxxxx
const date = (date = new Date()) => {
const day = date.getDate()
const month = date.getMonth() + 1
const year = date.getFullYear()
console.log(`${day}/${month}/${year}`)
}
//logs current Date
date()