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