在 JavaScript 中,我们可以使用内置的 Date 对象来获取当前时间。以下是一个简单的示例:
// 创建一个新的 Date 对象
let now = new Date();
// 获取当前的年份
let year = now.getFullYear();
// 获取当前的月份(注意:月份是从 0 开始计算的)
let month = now.getMonth() + 1;
// 获取当前的日期
let date = now.getDate();
// 获取当前的小时
let hours = now.getHours();
// 获取当前的分钟
let minutes = now.getMinutes();
// 获取当前的秒钟
let seconds = now.getSeconds();
// 将时间格式化为字符串
let currentTime = `${year}-${month}-${date} ${hours}:${minutes}:${seconds}`;