javascript 将分钟转换为秒

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8907764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 04:56:53  来源:igfitidea点击:

Convert Minutes to Seconds

javascriptexcel

提问by backdesk

I have a spreadsheet with values as follows:

我有一个电子表格,其值如下:

1:29.460

1:29.460

I need to convert it to

我需要将其转换为

89.460

89.460

I have a huge amount of these play offsets. I can either do it in excel or perhaps using javascript.

我有大量的这些游戏偏移量。我可以在 excel 中完成,也可以使用 javascript。

Any advice on a formula appreciated!

对公式的任何建议表示赞赏!

回答by Mathias Bynens

Here's a JavaScript solution:

这是一个 JavaScript 解决方案:

function convert(input) {
    var parts = input.split(':'),
        minutes = +parts[0],
        seconds = +parts[1];
    return (minutes * 60 + seconds).toFixed(3);
}

convert('1:29.460'); // '89.460'

回答by jfriend00

You can use code like this:

你可以使用这样的代码:

function toSeconds(str) {
    var pieces = str.split(":");
    var result = Number(pieces[0]) * 60 + Number(pieces[1]);
    return(result.toFixed(3));
}

console.log(toSeconds("1:29.460"));   // returns 89.460

回答by JMax

To give the whole picture, here is the VBA (UDF) solution:

为了给出全貌,这里是 VBA (UDF) 解决方案:

Public Function ConvertToSecond(r As Range)
Dim arr As Variant, lMinutes As Long, lSeconds As Double
Debug.Print (r.Value)
arr = Split(r.Value, ":")
lMinutes = arr(0)
lSeconds = CDbl(Replace(arr(1), ".", ","))
ConvertToSecond = Format(lMinutes * 60 + lSeconds, "#0.0##")
End Function

回答by George Reith

You are incorrectly formatting your cells, excel is storing your information as hh:mm.

您的单元格格式不正确,excel 将您的信息存储为 hh:mm。

You should add the hour column into your cell like so: 0:1:29.460and make sure it is set to the time format.

您应该像这样将小时列添加到单元格中:0:1:29.460并确保将其设置为时间格式。

Then in another column make sure the cell format is set to number and add the following formula (assuming your time is stored in the cell A1):

然后在另一列中确保单元格格式设置为数字并添加以下公式(假设您的时间存储在单元格 A1 中):

=A1*24*60*60

回答by Bon Espresso

Write following function in your VBA Modules

在 VBA 模块中编写以下函数

Public Function FormatMinutes(ByVal stime) As String
    Dim hour: hour = Mid(stime, 1, InStr(1, stime, ":") - 1)
    Dim minute: minute = CInt(Mid(stime, InStr(1, stime, ":") + 1, InStr(1, stime, ".") - InStr(1, stime, ":") - 1))
    FormatMinutes = (hour * 60 + minute) & Mid(stime, InStr(1, stime, "."), Len(stime))
End Function

To use it, just put =FormatMinute(A1)in formular.

要使用它,只需=FormatMinute(A1)输入公式即可。