将分钟转换为秒

Convert Minutes to Seconds

本文关键字:转换 分钟      更新时间:2023-09-26

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

1分29秒460

我需要将其转换为

89.460

我有大量的这种游戏补偿。我可以在excel中完成,也可以使用javascript。

任何关于配方的建议都将不胜感激!

这里有一个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'

您可以使用以下代码:

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

要了解全貌,下面是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

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

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

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

=A1*24*60*60

在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

要使用它,只需将=FormatMinute(A1)放在formular中即可。