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

2019-01-02 17:42发布

I am having similar requirement as this: Convert time in HH:MM:SS format to seconds only?

but in javascript. I have seen many examples of converting seconds into different formats but not HH:MM:SS into seconds. Any help would be appreciated.

11条回答
荒废的爱情
2楼-- · 2019-01-02 18:14

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>

查看更多
有味是清欢
3楼-- · 2019-01-02 18:15

try

time="12:12:12";
tt=time.split(":");
sec=tt[0]*3600+tt[1]*60+tt[2]*1;
查看更多
浪荡孟婆
4楼-- · 2019-01-02 18:16

This function works for MM:SS as well:

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])
        }
      }

查看更多
与风俱净
5楼-- · 2019-01-02 18:21

Try this:

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);
查看更多
冷夜・残月
6楼-- · 2019-01-02 18:22

This can be done quite resiliently with the following:

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

This is because each unit of time within the hours, minutes and seconds is a multiple of 60 greater than the smaller unit. Time is split into hour minutes and seconds components, then reduced to seconds by using the accumulated value of the higher units multiplied by 60 as it goes through each unit.

The +time is used to cast the time to a number.

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

查看更多
浅入江南
7楼-- · 2019-01-02 18:22

Javascript's static method Date.UTC() 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;
}
查看更多
登录 后发表回答