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

dev*_*rma 11 javascript momentjs

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

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

Niy*_*wan 29

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

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


小智 7

我将添加一项额外的信息:

当您使用 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
Run Code Online (Sandbox Code Playgroud)

当你使用 LT

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

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

更多详情请见:https : //momentjscom.readthedocs.io/en/latest/moment/04-displaying/01-format/#localized-formats

我希望它可以帮助