原文: Định dạng ngày trong JavaScript – Cách định dạng ngày trong JS,Người viết: Joel Olawanle
译者: Miya Liu
JavaScript được sử dụng trong các trang web của người dùng JavaScript.
在创建网页时,在某些时候,你可能会出于原因需要使用日期——例如显示某些内容的上字字、、
在本文中,你将学习如何在 JavaScript 中设置日期格式,并了解如何使用流行的 JavaScript 日期库 moment.js 来执行此ん作
如何在 JavaScript 中获取日期
在 JavaScript 中,你可以使用 new Date()
或 Date()
构造函数来获取日期(当前日期或特定日期)。
new Date()
构造函数返回一个新的 Date
对象,而 Date()
构造函数返回当前日期和时间的字符串形式。
let stringDate = Date();
console.log(stringDate); // "Tue Aug 23 2022 14:47:12 GMT-0700 (Pacific Daylight Time)"
let objectDate = new Date();
console.log(objectDate); // Tue Aug 23 2022 14:47:12 GMT-0700 (Pacific Daylight Time)
这些完整格式的日期包括日、月、年、分钟、小时等等。
你 你 你 , , , , , , 其 其 其 其 到 到 到 到 到 到 所有
如何在 JavaScript 中格式化日期
格式化 日期 的 某些 某些 国家 国家 国家 国家 国家 , 年份 年份 年份 年份 年份 年份 年份 年份 年份 年份 年份 年份 年份 年份 年份 日期 在 , , (( /2022)……
无论格式如何,你都希望分解日期对象值并获取你想要的网页所需信息。
这可以通过 JavaScript 日期方法实现。这些方法有很多,但由于你只关心本文中的日期,因此你只需誁䅶丶:
getFullYear()
– 你将使用此方法以四位数字(yyyy)形式获取年份。例如,2022 年。getMonth()
– 你将使用此方法将月份作为 0-11 之间的数字获取,每个数字代表从一月到十二月的月份。例如2
将是 March 的索引,因为它是从零开始的索引(意味着它从开始)。
getDate()
– 你 使用 方法 1-31 之间 数字 获取 日期 这 这 , , , 而 是 确切 的 , , , 因此 1
Lời bài hát có nghĩa là:这些方法只能与 new Date()
构造函数一起使用,该构造函数返回一个日期对象。
let objectDate = new Date();
let day = objectDate.getDate();
console.log(day); // 23
let month = objectDate.getMonth();
console.log(month + 1); // 8
let year = objectDate.getFullYear();
console.log(year); // 2022
你会注意到上面的 month
值添加了 1
。这是因为月份是 索引的,该值从
开始。这意味着八月的索引是
7
,而不是 8
。
此时,你已经能够从日期对象中提取所有日期值。你现在可以以你想要的任何格式组织它们:
let format1 = month + "/" + day + "/" + year;
console.log(format1); // 7/23/2022
let format2 = day + "/" + month + "/" + year;
console.log(format2); // 23/7/2022
let format3 = month + "-" + day + "-" + year;
console.log(format3); // 7-23-2022
let format4 = day + "-" + month + "-" + year;
console.log(format4); // 23-7-2022
在上面,你使用加号运算符连接了这些值。你还可以使用模板字面量来连接它们:
let format1 = `${month}/${day}/${year}`;
console.log(format1); // 7/23/2022
let format2 = `${day}/${month}/${year}`;
console.log(format2); // 23/7/2022
let format3 = `${month}-${day}-${year}`;
console.log(format3); // 7-23-2022
let format4 = `${day}-${month}-${year}`;
console.log(format4); // 23-7-2022
现在,你已经看到了格式化日期的可能方式。
另 种 可能 , 如果 从 从 从 (从 从 从 从 从 , , , , , , , , 日期 日期
if (day < 10) {
day = '0' + day;
}
if (month < 10) {
month = `0${month}`;
}
let format1 = `${month}/${day}/${year}`;
console.log(format1); // 07/23/2022
JavaScript 中格式化日期的其他方式感兴趣?请查看我的文章“如何用一行代码在 JavaScript 中格式化日期”.
Sử dụng Moment.js và JavaScript 中格式化日期
Moment.js không phải là ngôn ngữ lập trình JavaScript
要使用此库,你必须使用文档中的任何首选项将包安装到你的项目中。
对于本指南,你只关心如何使用 Moment.js 格式化日期:
let date = moment().format();
console.log(date); // 2022-08-23T16:50:22-07:00
要格式化此日期/时间值,你所要做的就是输入你喜欢的格式,它将返回格式化的日期,如下牀:
let dateFormat1 = moment().format('D-MM-YYYY');
console.log(dateFormat1); // 23-08-2022
let dateFormat2 = moment().format('D/MM/YYYY');
console.log(dateFormat2); // 23/08/2022
let dateFormat3 = moment().format('MM-D-YYYY');
console.log(dateFormat3); // 08-23-2022
let dateFormat4 = moment().format('MM/D/YYYY');
console.log(dateFormat4); // 08/23/2022
小结
Bạn có thể sử dụng ứng dụng này để tạo ra các ứng dụng moment.js và JavaScript.
将库 用 , , , , 这 这 这 这 处理 处理 处理 更 更 更 多 多 多 , , , 你 你
我 建议 从头 的 的 简单 , , , , , , , , , , , , 该 该
祝你编程愉快!