// 获取当前的年月日时分秒
function getDateTime() {
const now = new Date(); // 创建一个表示当前日期和时间的Date对象
const year = now.getFullYear(); // 获取年份
const month = String(now.getMonth() + 1).padStart(2, '0'); // 获取月份(从0开始,所以要加1),并确保两位数
const day = String(now.getDate()).padStart(2, '0'); // 获取日期,并确保两位数
const hours = String(now.getHours()).padStart(2, '0'); // 获取小时,并确保两位数
const minutes = String(now.getMinutes()).padStart(2, '0'); // 获取分钟,并确保两位数
const seconds = String(now.getSeconds()).padStart(2, '0'); // 获取秒数,并确保两位数
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`; // 返回格式化的日期时间字符串
}
console.log(getDateTime()); // 输出类似 "2023-10-05 14:30:45" 的字符串
下一篇:js 获取当月第一天
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站