将分钟转换为秒

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

1:29.460

我需要将其转换为

89.460

我有这么多的偏移量。 我可以在Excel中或使用JavaScript。

任何意见的公式赞赏!

这是一个JavaScript解决scheme:

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)); } toSeconds("1:29.460"); // returns 89.460 

你可以看到它在这里工作: http : //jsfiddle.net/jfriend00/RrZ4W/

为了说明问题,下面是VBA(UDF)解决scheme:

 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模块中写下以下function

 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中。