0

I have a need to convert microseconds to a human readable format. While I would be able convert it to HH:MM:SS.MS, I'm not sure the best way to strip the 00:'s if present I would like work with these

input = [ 12040000, 60042000, 15582000000 ]
output = [ '12.04', '1:00.42', '4:19:27' ]

function formatMark(mu) {
    var timeList = []
    var ms = mu / 1000
    var time = new Date(ms)

    var ms = time.getMilliseconds()
    var ss = time.getSeconds()
    var mm = time.getMinutes()
    var hh = time.getHours()

    if (ms > 1) timeList.push(ms) //
    if (ss > 1) timeList.push(ss) // 
    if (mm > 1) timeList.push(mm) //
    if (hh > 1) timeList.push(hh) //

    return { hh + ':' + mm + ':' + ss + '.' + ms }

 }

Unfortunately the code above doesn't work, probably for multiple reasons in which I'd be better off knowing, but I really just need a conventional and readable output. Any tips / suggestions are appreciated!

Chris
  • 609
  • 3
  • 9
  • 21

2 Answers2

0

Unfortunately the code above doesn't work, probably for multiple reasons in which I'd be better off knowing,

Syntax of your return statement is wrong

Try replacing your return statement with

return { hh : hh, mm : mm, ss : ss, ms : ms }

Finally, use the output of formatMark as

var output = formatMark(157442234333332221);
var finalOutput = Object.keys( output ).map( function( key ){ return key + ":" + output[key] } ).join(",");
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
0

As per your question, you are passing parameter as array type but the function is not compatible to handle this, your logic for calculating readable time string is correct. only thing is that you need to handle this for array type parameter.

function formatMark(mu) {
     var timeList = []
     mu.map(function(t){
           var ms = mu / 1000
           var time = new Date(ms)
           var ms = time.getMilliseconds()
           var ss = time.getSeconds()
           var mm = time.getMinutes()
           var hh = time.getHours()

           if (ms > 1) timeList.push(ms) //
           if (ss > 1) timeList.push(ss) // 
           if (mm > 1) timeList.push(mm) //
           if (hh > 1) timeList.push(hh) //
           var timeString=hh + ':' + mm + ':' + ss + '.' + ms 
           console.log(timeString)
           timeList.push(timeString)
      })
      return timeList;
}

This function accepts array and returns the array of readable time strings.

Jaydip Jadhav
  • 12,179
  • 6
  • 24
  • 40