如何在moment.js中格式化自定义时间

How to format custom time in moment.js?

本文关键字:格式化 自定义 时间 js moment      更新时间:2023-09-26

我试过moment.time(column.start_time).format("hh:mm A"),但它给我错误。

我有自定义的时间字段值是"15:30:00"想要格式像"03:30 PM"。

您需要在moment函数中添加格式字符串,因为您的日期不是有效的ISO日期。

var time = "15:30:00";
var formatted = moment(time, "HH:mm:ss").format("hh:mm A");
console.log(formatted); 
<script src="https://momentjs.com/downloads/moment.min.js"></script>

我将包括一个额外的信息:

当您使用hh:mm时A

var time = "15:30:00";
var formatted = moment(time, "HH:mm").format("hh:mm A");
console.log(formatted); 
//it will return 03:30 PM

当你使用LT

var time = "15:30:00";
var formatted = moment(time, "HH:mm:ss").format("LT");
console.log(formatted); 
//it will return 3:30 PM

当第一个数字小于10时,它们之间的差值只是第一个数字前的0。我正在使用日期时间选择器,然后我明白了为什么它没有绑定在我的组合框中。

更多细节见:https://momentjscom.readthedocs.io/en/latest/moment/04-displaying/01-format/本地化格式

我希望它能帮到你