// 获取当前时间的示例代码
// 使用 JavaScript 的 Date 对象来获取当前时间
const now = new Date();
// 获取年份
const year = now.getFullYear();
// 获取月份(注意:JavaScript 中的月份是从 0 开始的,所以需要加 1)
const month = now.getMonth() + 1;
// 获取日期
const date = now.getDate();
// 获取小时
const hours = now.getHours();
// 获取分钟
const minutes = now.getMinutes();
// 获取秒
const seconds = now.getSeconds();
// 格式化输出当前时间
const formattedDate = `${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(formattedDate);
new Date()
:创建一个新的 Date
对象,表示当前的日期和时间。getFullYear()
:获取当前的年份。getMonth()
:获取当前的月份(从 0 开始,因此需要加 1)。getDate()
:获取当前的日期。getHours()
:获取当前的小时。getMinutes()
:获取当前的分钟。getSeconds()
:获取当前的秒。String(...).padStart(2, '0')
:确保月份、日期、小时、分钟和秒数都是两位数,不足两位时前面补零。formattedDate
:将上述获取的时间信息格式化为 YYYY-MM-DD HH:MM:SS
的字符串形式,并打印出来。上一篇:js 获取当前地址
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站