最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to create full time string from hours and minutes in Moment.js? - Stack Overflow

programmeradmin7浏览0评论

I have time strings having only hours and minutes, for example '19:39', '21:37', '14:10'.

I'd like to put such a string in a moment.js function like format() and get a string with seconds: '19:39:00', '21:37:00', etc. The seconds value will be always '00'. I tried with the format function but it returns Invalid date all the time.

I have time strings having only hours and minutes, for example '19:39', '21:37', '14:10'.

I'd like to put such a string in a moment.js function like format() and get a string with seconds: '19:39:00', '21:37:00', etc. The seconds value will be always '00'. I tried with the format function but it returns Invalid date all the time.

Share Improve this question asked Aug 19, 2019 at 9:47 Hubert KubiakHubert Kubiak 6451 gold badge11 silver badges35 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

Something like this?

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"));
<script src="https://momentjs./downloads/moment.js"></script>

Here's a reference to the string format options

I think it's no need to use moment if you just need to get a string

`${'19:39'}:00`

will be enough, right?

Btw, moment('19:39', 'H:mm').format('H:m:ss') will do the job.

发布评论

评论列表(0)

  1. 暂无评论