function getCurrentDateTime() {
const date = new Date();
const year = date.getFullYear();
const month = ("0" + (date.getMonth() + 1)).slice(-2);
const day = ("0" + date.getDate()).slice(-2);
const hours = ("0" + date.getHours()).slice(-2);
const minutes = ("0" + date.getMinutes()).slice(-2);
const seconds = ("0" + date.getSeconds()).slice(-2);
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
console.log(getCurrentDateTime()); // 输出格式为 "yyyy-mm-dd"
在这个示例中,getCurrentDate 函数首先创建一个 Date 对象,然后获取年份、月份和日期,最后将它们按照 “yyyy-mm-dd hh:mm:ss” 的格式输出。