xxxxxxxxxx
const date = new Date().toLocaleDateString()
console.log(date)
// Output
// 5/23/2022 -> mm/dd/yyyy
const dateArray = date.split("/")
const dateArray.map(d => d.length == 1 && d.length != 4 ? "0"+d : d)
const formatedDate = `${day}.${dateArray[0]}.${dateArray[2]}`
console.log(formatedDate)
// Output
// 23.05.2022 -> dd.mm.yyyy
xxxxxxxxxx
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();
today = mm + '/' + dd + '/' + yyyy;
document.write(today);
xxxxxxxxxx
// `date` is a `Date` object
const formatYmd = date => date.toISOString().slice(0, 10);
// Example
formatYmd(new Date()); // 2020-05-06
xxxxxxxxxx
const yourDate = new Date()
yourDate.toISOString().split('T')[0]
xxxxxxxxxx
const yourDate = new Date()
yourDate.toISOString().split('T')[0]
xxxxxxxxxx
const date = new Date();
const formattedDate = date.toISOString().slice(0, 10);
console.log(formattedDate);
// Output: 2023-05-07
xxxxxxxxxx
function formatDate(date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
const seconds = String(date.getSeconds()).padStart(2, '0');
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
// Get the current date and time
const currentDate = new Date();
// Format the date using the defined function
const formattedDate = formatDate(currentDate);
console.log(formattedDate); // Example output: "2022-01-31 09:15:30"