// 获取当前时间的示例代码
// 使用 JavaScript 的 Date 对象来获取当前时间
const now = new Date();
// 获取年份
const year = now.getFullYear();
// 获取月份(注意:月份是从0开始的,所以需要加1)
const month = now.getMonth() + 1;
// 获取日期
const date = now.getDate();
// 获取小时
const hours = now.getHours();
// 获取分钟
const minutes = now.getMinutes();
// 获取秒数
const seconds = now.getSeconds();
// 格式化时间为 YYYY-MM-DD HH:MM:SS 格式
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);
new Date()
创建一个表示当前日期和时间的对象。getFullYear()
、getMonth()
、getDate()
等方法分别获取年、月、日等信息。String().padStart(2, '0')
方法进行补零处理。上一篇:js 取当前时间
下一篇:js date 加一天
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站