如何在 Moment.js 中从小时和分钟创建完整时间字符串?

Hub*_*iak 0 javascript date momentjs

我的时间字符串只有小时和分钟,例如“19:39”、“21:37”、“14:10”。

我想将这样的字符串放入 moment.js 函数中,例如format()并获取带秒的字符串:'19:39:00'、'21:37:00'等。秒值将始终为'00' 。我尝试使用该format函数,但它Invalid date总是返回。

Lia*_*iam 6

像这样的东西吗?

const str = "19:39";
//parse string to moment using format string to dictate what the string constains
const m = moment(str, "H:mm");
//output moment in new format
console.log(m.format("H:m:ss"));
Run Code Online (Sandbox Code Playgroud)
<script src="https://momentjs.com/downloads/moment.js"></script>
Run Code Online (Sandbox Code Playgroud)

这是字符串格式选项的参考