卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章64336本站已运行4115

js如何获得年月日

如何获取 javascript 中的年月日:获取当前年月日:使用 date 对象的 getfullyear(), getmonth(), getdate() 方法。获取特定日期的年月日:使用 date 构造函数,传入时间戳或日期字符串。获取特定时间戳的年月日:使用 new date(timestamp) 获取 date 对象,然后使用 getfullyear(), getmonth(), getdate() 方法。获取当前时间戳的年月日:使用 date.now() 获取当前时间戳,然后使用 ne

js如何获得年月日

如何在 JavaScript 中获取年月日

获取当前年月日:

使用 Date 对象的 getFullYear(), getMonth() 和 getDate() 方法。

const now = new Date();
const year = now.getFullYear();
const month = now.getMonth() + 1; // 月份从 0 开始,因此需要加 1
const day = now.getDate();

console.log(`${year}-${month}-${day}`);

获取特定日期的年月日:

使用 Date 对象的构造函数,传入时间戳或日期字符串。

const timestamp = 1659878400000; // 2022 年 8 月 15 日 16:00:00
const date = new Date(timestamp);
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();

console.log(`${year}-${month}-${day}`);

获取特定时间戳的年月日:

使用 new Date(timestamp) 获取 Date 对象,然后使用 getFullYear(), getMonth() 和 getDate() 方法提取年月日。

const timestamp = 1659878400000;
const date = new Date(timestamp);
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();

console.log(`${year}-${month}-${day}`);

获取当前时间戳的年月日:

使用 Date.now() 获取当前时间戳,然后使用 new Date(timestamp) 获取 Date 对象,最后再使用 getFullYear(), getMonth() 和 getDate() 方法提取年月日。

const timestamp = Date.now();
const date = new Date(timestamp);
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();

console.log(`${year}-${month}-${day}`);
卓越飞翔博客
上一篇: js list如何取值
下一篇: js如何声明数组
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏