Jacob Rush
Jacob Rush

Reputation: 31

time convert in javascript

How does one convert the following to seconds in JavaScript?

mm:ss.sss ?

What is the .sss?

Upvotes: 2

Views: 231

Answers (5)

user1170618
user1170618

Reputation:

var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
document.write(month + "/" + day + "/" + year)

Upvotes: 0

Christian C. Salvadó
Christian C. Salvadó

Reputation: 827256

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.

Upvotes: 4

Peter Hansen
Peter Hansen

Reputation: 22047

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');

Upvotes: 1

miku
miku

Reputation: 188004

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

Upvotes: 0

BrainCore
BrainCore

Reputation: 5412

".sss" is milliseconds

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

Upvotes: 2

Related Questions