Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

js 获取年月日

作者:古枕清风   发布日期:2026-03-28   浏览:78

// 获取当前的年月日

// 创建一个新的 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 获取年月

大家都在看

js 数组打乱顺序

js 两个数组取交集

js 数组对象排序

js 对象数组排序

js 数组删掉第一个值

js fill

js 数组连接

js json数组

js 数组复制

js 复制数组

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站