Format hours, minutes, and seconds with a moment. Js

I get this value from my internal service: 171054 . It represents hh:mm:ss . But when I use the formatting options from the documents, it returns me 00:00:00 .

Things I tried:

 moment('171054').format('hh-mm-ss') moment('171054').format('HH-mm-ss') moment('171054').format('HH-MM-SS') 
+7
source share
2 answers

You are misleading format with the parsing option. Since your input string does not conform to the ISO 8601 format, you must specify the format when parsing .

Here is a working example for your use case:

 var mom = moment('171054', 'HHmmss'); console.log(mom.format()); console.log(mom.format('HH:mm:ss')); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.min.js"></script> 
+19
source

I'm not sure that momentjs can read this as a date since there is no identifier. I would suggest changing the value, for example, you have 171054, get every 2 digits since you are sure that it will display as hh: mm: ss, then add an identifier between such as ":" or "-" , and then try formatting the characters again. / p>

+1
source

Source: https://habr.com/ru/post/1258798/


All Articles