gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:34:05 26 4
gpt4 key购买 nike

如何从 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com