// 获取当前时间的示例代码
// 使用 new Date() 创建一个表示当前日期和时间的 Date 对象
const now = new Date();
// 获取当前的年份
const year = now.getFullYear();
// 获取当前的月份(注意:月份是从 0 开始的,所以需要加 1)
const month = now.getMonth() + 1;
// 获取当前的日期
const date = now.getDate();
// 获取当前的小时数(24 小时制)
const hours = now.getHours();
// 获取当前的分钟数
const minutes = now.getMinutes();
// 获取当前的秒数
const seconds = now.getSeconds();
// 格式化输出当前时间
const formattedTime = `${year}-${String(month).padStart(2, '0')}-${String(date).padStart(2, '0')} ${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
console.log(formattedTime); // 输出格式化后的时间,例如:2023-10-05 14:30:45
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站