views:

100

answers:

4

How does one convert the following to seconds in JavaScript?

mm:ss.sss ?

What is the .sss?

+1  A: 

".sss" is milliseconds

If you only want seconds, then simply extract the "ss" portion.

BrainCore
A: 

a quick tutorial an javascript Date: http://www.tizag.com/javascriptT/javascriptdate.php

The MYYN
+1  A: 

Possibly you're asking about parsing that information, from user input? Would this be along the lines of what you need?

var input = '22:33.123';
var parts = /(\d\d):(\d\d.\d\d\d)/.exec(input);
function convert(parts) {
    return parseInt(parts[1]) * 60 + parseFloat(parts[2]);
}
alert(input + ' is ' + convert(parts)  + ' seconds');
Peter Hansen
+3  A: 

You could do something like this:

function getSeconds(timestamp) {
  var parts = timestamp.match(/\d+/g),
      minute = 60,
      hour = 60 * minute,
      ms = 1/1000;

  return (parts[0]*hour) + (parts[1]*minute) + (+parts[2]) + (parts[3]*ms);
}

getSeconds('01:33:33.235'); // 5613.235 seconds

Edited since you need the milliseconds.

CMS
+1 Great answer
Doug Neiner
Thanks @dcneiner!
CMS