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

时间:2019-05-14 12:53:41

标签: javascript momentjs moment-timezone

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

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

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

我希望获得与使用shortTime在angular中使用的结果类似的结果:

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

1 个答案:

答案 0 :(得分:1)

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

  

由于首选格式因地区而异,因此有一些标记可用于根据时区设置格式化时刻。

     

相同格式有大小写不同。小写版本旨在作为大写版本的缩写。

这里有一个现场样本:

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>