2

我需要在Javascript中将小时:分钟(00:00)转换为分钟00。

我考虑过使用 substr 分别获取小时和分钟,然后将小时部分乘以 60,然后添加分钟部分。

有没有其他简单的方法可以做到这一点?

4

2 回答 2

5

It's pretty easy with split:

var str = "04:17";
var parts = str.split(":");
var minutes = parseInt(parts[0], 10) * 60 + parseInt(parts[1], 10);
console.log(minutes); // 257 (four hours and seventeen minutes)
于 2012-12-29T11:05:46.337 回答
0

To split in hour and minute, you can use the split() function on the String object:

"12:05".split(':');

--> ["12", "05"]

Then you need to convert the Strings in the array to integers with parseInt:

var hours = parseInt("12", 10);
var minutes = parseInt("05", 10);

Then rest is simple calculation:

var total = hours * 60 + minutes;
于 2012-12-29T11:01:12.880 回答