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"
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 date = new Date();
const formattedDate = date.toISOString().slice(0, 10);
console.log(formattedDate);
// Output: 2023-05-07
xxxxxxxxxx
const yourDate = new Date()
yourDate.toISOString().split('T')[0]
xxxxxxxxxx
const date = new Date();
const formattedDate = date.toISOString().slice(0, 19).replace('T', ' ');
console.log(formattedDate);
// Output: 2023-05-07 06:22:28