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.
2 Answers
Reset to default 6Something 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.