javascript - 使用 moment.js 本地化短时间字符串

标签 javascript momentjs moment-timezone

如何从 moment.js 对象获取与当前语言环境相关的时间字符串?

moment().format('HH:mm') 

无论本地化如何,总是得到相同的结果。

我想要与使用 shortTime 在 Angular 中使用的结果类似的结果:

formatDate(new Date(), 'shortTime'): // HH:mm, resp hh:mm a

最佳答案

您可以简单地使用 format() 中列出的本地化格式文档

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.

这里是一个活生生的例子:

console.log( moment().format('LT') );
moment.locale('it');
console.log( moment().format('LT') );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment-with-locales.min.js"></script>

关于javascript - 使用 moment.js 本地化短时间字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56130986/

相关文章:

javascript - 我如何知道打印机是否通过 JavaScript 被取消?

javascript - IE8 jquery ajax head 请求不起作用?

javascript - Commander.js 收集多个选项总是包括默认

javascript - html css jquery modal 无法在浏览器中打开

javascript - 在 momentjs 中显示没有年份的长日期格式

javascript - Angular Date 管道,以及一个带有 Momentjs 的简单计时器

javascript - 将 MM/DD/YYYY 日期转换为月日年

javascript - 时分秒两个时间戳之间的差异

momentjs - 使用moment js生成带有动态VTIMEZONE的ics

javascript - Momentjs 日期值转换为日期时间格式