// 获取当前的年月日
// 创建一个新的 Date 对象,表示当前日期和时间
const now = new Date();
// 获取年份
const year = now.getFullYear(); // getFullYear() 方法返回一个四位数的年份
// 获取月份(注意:getMonth() 返回的是 0-11,所以需要加 1)
const month = now.getMonth() + 1;
// 获取日期
const day = now.getDate(); // getDate() 方法返回一个月中的某一天 (1-31)
// 将结果格式化为字符串输出
console.log(`今天的日期是: ${year}年${month}月${day}日`);
// 如果你需要将月份和日期格式化为两位数,可以使用以下方法:
const formattedMonth = String(month).padStart(2, '0'); // 确保月份为两位数
const formattedDay = String(day).padStart(2, '0'); // 确保日期为两位数
console.log(`今天的日期是: ${year}-${formattedMonth}-${formattedDay}`);
上一篇:js 获取年月日时分秒
下一篇:js 获取年月
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站