Convert HH: MM: SS string in seconds to javascript only

I have a similar requirement: Convert time in HH: MM: SS format for only seconds?

but in javascript. I saw many examples of converting seconds to different formats, but not HH: MM: SS in seconds. Any help would be greatly appreciated.

+48
javascript time
Mar 09 2018-12-12T00:
source share
11 answers

Try the following:

var hms = '02:04:33'; // your input string var a = hms.split(':'); // split it at the colons // minutes are worth 60 seconds. Hours are worth 60 minutes. var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]); console.log(seconds); 
+104
Mar 09 '12 at 20:15
source share

This function transmits “HH: MM: SS”, as well as “MM: SS” or “SS”.

 function hmsToSecondsOnly(str) { var p = str.split(':'), s = 0, m = 1; while (p.length > 0) { s += m * parseInt(p.pop(), 10); m *= 60; } return s; } 
+46
Mar 09 '12 at 20:18
source share

This can be done fairly steadily with the following:

 '01:02:03'.split(':').reduce((acc,time) => (60 * acc) + +time); 

This is because each unit of time in hours, minutes, and seconds is a multiple of 60 more than the smaller unit. Time is divided into component minutes and seconds, and then reduced to several seconds using the accumulated value of higher units times 60 when it passes through each block.

+time used to indicate the time of a number.

It basically ends with: (60 * ((60 * HHHH) + MM)) + SS

+7
Jul 25 '17 at 1:49 on
source share

to try

 time="12:12:12"; tt=time.split(":"); sec=tt[0]*3600+tt[1]*60+tt[2]*1; 
+6
Mar 09 '12 at 20:16
source share

Since the getTime function of a Date object gets milliseconds since 1970/01/01, we can do this:

 var time = '12:23:00'; var seconds = new Date('1970-01-01T' + time + 'Z').getTime() / 1000; 
+6
Mar 15 '13 at 18:57
source share

Convert the string hh:mm:ss in seconds to one line. The format h:m:s and mm:ss , m:s , etc. are also allowed.

 '08:45:20'.split(':').reverse().reduce((prev, curr, i) => prev + curr*Math.pow(60, i), 0) 
+6
Nov 10 '16 at 13:35
source share

The static Javascript Date.UTC() method does the trick:

 alert(getSeconds('00:22:17')); function getSeconds(time) { var ts = time.split(':'); return Date.UTC(1970, 0, 1, ts[0], ts[1], ts[2]) / 1000; } 
+4
Jun 14 '13 at 8:06 on
source share

 function parsehhmmsst(arg) { var result = 0, arr = arg.split(':') if (arr[0] < 12) { result = arr[0] * 3600 // hours } result += arr[1] * 60 // minutes result += parseInt(arr[2]) // seconds if (arg.indexOf('P') > -1) { // 8:00 PM > 8:00 AM result += 43200 } return result } $('body').append(parsehhmmsst('12:00:00 AM') + '<br>') $('body').append(parsehhmmsst('1:00:00 AM') + '<br>') $('body').append(parsehhmmsst('2:00:00 AM') + '<br>') $('body').append(parsehhmmsst('3:00:00 AM') + '<br>') $('body').append(parsehhmmsst('4:00:00 AM') + '<br>') $('body').append(parsehhmmsst('5:00:00 AM') + '<br>') $('body').append(parsehhmmsst('6:00:00 AM') + '<br>') $('body').append(parsehhmmsst('7:00:00 AM') + '<br>') $('body').append(parsehhmmsst('8:00:00 AM') + '<br>') $('body').append(parsehhmmsst('9:00:00 AM') + '<br>') $('body').append(parsehhmmsst('10:00:00 AM') + '<br>') $('body').append(parsehhmmsst('11:00:00 AM') + '<br>') $('body').append(parsehhmmsst('12:00:00 PM') + '<br>') $('body').append(parsehhmmsst('1:00:00 PM') + '<br>') $('body').append(parsehhmmsst('2:00:00 PM') + '<br>') $('body').append(parsehhmmsst('3:00:00 PM') + '<br>') $('body').append(parsehhmmsst('4:00:00 PM') + '<br>') $('body').append(parsehhmmsst('5:00:00 PM') + '<br>') $('body').append(parsehhmmsst('6:00:00 PM') + '<br>') $('body').append(parsehhmmsst('7:00:00 PM') + '<br>') $('body').append(parsehhmmsst('8:00:00 PM') + '<br>') $('body').append(parsehhmmsst('9:00:00 PM') + '<br>') $('body').append(parsehhmmsst('10:00:00 PM') + '<br>') $('body').append(parsehhmmsst('11:00:00 PM') + '<br>') 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
0
Jul 14 '15 at 17:34
source share

You can do this dynamically - if you come across not only HH: mm: ss, but also mm: ss or even ss.

 var str = '12:99:07'; var times = str.split(":"); times.reverse(); var x = times.length, y = 0, z; for (var i = 0; i < x; i++) { z = times[i] * Math.pow(60, i); y += z; } console.log(y); 
0
Apr 21 '16 at 0:59
source share

This function also works for MM: SS:

 const convertTime = (hms) => { if (hms.length <3){ return hms } else if (hms.length <6){ const a = hms.split(':') return hms = (+a[0]) * 60 + (+a[1]) } else { const a = hms.split(':') return hms = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]) } } 
0
Mar 16 '17 at 8:19
source share
 new Date(moment('23:04:33', "HH:mm")).getTime() 

Output: 1499755980000 (in milliseconds) (1499755980000/1000) (second)

Note: this output computes diff from 1970-01-01 12: 0: 0 so far and we need to implement moment.js

0
Jul 11 '17 at 7:27
source share



All Articles