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

js 获取今天年月日

作者:残缺的不完美   发布日期:2025-07-01   浏览:32

// 获取今天的年月日

// 使用 JavaScript 的 Date 对象来获取当前的日期信息
const today = new Date();

// 获取年份,getFullYear() 方法返回 4 位数的年份
const year = today.getFullYear();

// 获取月份,getMonth() 方法返回的是 0-11 的数字,因此需要加 1 来得到实际的月份
const month = today.getMonth() + 1;

// 获取日期,getDate() 方法返回的是 1-31 的数字
const day = today.getDate();

// 将年月日组合成字符串,使用 padStart 方法确保月份和日期都是两位数
const formattedDate = `${year}-${String(month).padStart(2, '0')}-${String(day).padStart(2, '0')}`;

console.log(formattedDate); // 输出格式如:2023-10-05

上一篇:js jsonp

下一篇:js 深拷贝数组

大家都在看

js 数组对象排序

js 数组删掉第一个值

js fill

js 数组复制

js 复制数组

js 数组拷贝

js 对象转数组

js 深拷贝数组

js 获取今天年月日

js jsonp

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

Laravel 中文站