简体   繁体   English

moment.js格式化日期错误

[英]moment.js formatting date wrong

I'm trying to get upcoming date using moment.js . 我正在尝试使用moment.js获取即将到来的日期。 The date is obtained, but it's formatted wrongly. 日期已获得,但格式错误。 According to docs, format('l') formats the date in dd/mm/yyyy format and format('L') formats date in mm/dd/yyyy . 根据文档, format('l')将日期格式化为dd/mm/yyyy格式, format('L')格式将日期格式化为mm/dd/yyyy but I'm getting the same output for both. 但是我得到的都是相同的输出。 Here is my code. 这是我的代码。

let next = moment().add(30, 'days').format('l'); // Output 10/16/2019
let next = moment().add(30, 'days').format('L'); // Output 10/16/2019
let next = moment().add(30, 'days').format('DD/MM/YYYY'); // Works fine

I tried i with moment.js v2.24.0 what's wrong in here? 我用moment.js v2.24.0尝试了moment.js v2.24.0是什么问题? am I missing something or doing it wrong way? 我错过了什么还是做错了吗?

According to docs, format('l') formats the date in dd/mm/yyyy format and format('L') formats date in mm/dd/yyyy . 根据文档, format('l')将日期格式化为dd/mm/yyyy格式, format('L')格式将日期格式化为mm/dd/yyyy

No, that isn't what the documentation says : 不,那不是文档所说的

Localized formats 本地化格式

Because preferred formatting differs based on locale, there are a few tokens that can be used to format a moment based on its locale. 由于首选格式会根据区域设置而有所不同,因此有一些标记可用于根据时刻的区域设置格式化时间。

There are upper and lower case variations on the same formats. 相同格式有大小写不同。 The lowercase version is intended to be the shortened version of its uppercase counterpart. 小写版本旨在作为大写版本的简化版本。

 Month numeral, day of month, year: L 09/04/1986 月份数字,月份中的某天,年份:L 09/04/1986\n                                     l 9/4/1986 l 9/4/1986\n

There's nothing there saying L will use one field order and l another. 有没有什么说法L会使用一个字段顺序和l另一个。

It's using your current Moment locale to format the date. 它使用您当前的Moment语言环境来格式化日期。 See the i18n section for how to set locales, globally or per instance. 有关如何全局或按实例设置语言环境的信息,请参见“ i18n”部分

According to docs 根据文档

moment().format('L'); moment()。format('L'); // 09/16/2019 moment().format('l'); // 09/16/2019 moment()。format('l'); // 9/16/2019 // 9/16/2019

https://momentjs.com/ https://momentjs.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM