繁体   English   中英

我们可以将20:72:84(hh:mm:ss)的持续时间转换为21:13:24(hh:mm:ss)的格式而无需循环吗?

[英]Can we convert such 20:72:84(hh:mm:ss) duration to 21:13:24 (hh:mm:ss) format Without Loop?

在VBScript中是否可以将20:72:84(hh:mm:ss)持续时间转换为21:13:24(hh:mm:ss)格式?

是的,显然我们可以通过它循环播放,但是我想避免这种Loopy技术。

根据Siddharth解决方案-我刚刚修改了代码,使其适合VBScript platform

Option Explicit


    Dim S
    S=Convert("23:61:61")
    MsgBox(s)


Function Convert(strTime) 'As String
    Dim timeArray() 
    Dim h , m , s 

    MsgBox("Hi")

    'On Error Resume Next

    timeArray = Split(strTime, ":")

    h = timeArray(0): m = timeArray(1): s = timeArray(2)

    REM If err then

        REM MsgBox(err)
        REM err.clear
        REM Exit Function
    REM End if

    Do Until s < 60
        s = s - 60
        m = m + 1
    Loop

    Do Until m < 60
        m = m - 60
        h = h + 1
    Loop

    Do Until h < 24
        h = h - 24
    Loop

    Convert = Format(h, "00") & ":" & Format(m, "00") & ":" & Format(s, "00")

    'on Error Goto 0
    'Exit Function
'Whoa:
    'Convert = "Error! CYDD!" '<~~ CYDD : Check Your Data Dude :)
End Function

EDIT1我遇到错误,因为Type mismatch与行timeArray = Split(strTime, ":") Type mismatch

谢谢,

这是您要尝试的吗?

Option Explicit

Sub Sample()
    Debug.Print Convert("23:61:61")
    Debug.Print Convert("24:61:61")
    Debug.Print Convert("20:72:84")
    Debug.Print Convert("Hello World")
End Sub

Function Convert(strTime As String) As String
    Dim timeArray() As String
    Dim h As Long, m As Long, s As Long

    On Error GoTo Whoa

    timeArray = Split(strTime, ":")

    h = timeArray(0): m = timeArray(1): s = timeArray(2)

    Do Until s < 60
        s = s - 60
        m = m + 1
    Loop

    Do Until m < 60
        m = m - 60
        h = h + 1
    Loop

    Do Until h < 24
        h = h - 24
    Loop

    Convert = Format(h, "00") & ":" & Format(m, "00") & ":" & Format(s, "00")

    Exit Function
Whoa:
    Convert = "Error! CYDD!" '<~~ CYDD : Check Your Data Dude :)
End Function

快照

在此处输入图片说明

编辑(跟进)

我上面提供的代码用于VBA-Excel因为它是您的标签之一

对于VB-Script ,请使用此代码

MsgBox Convert("23:61:61")
MsgBox Convert("24:61:61")
MsgBox Convert("20:72:84")
MsgBox Convert("Hello World")

Function Convert(strTime)
    Dim timeArray
    Dim h, m, s, hh, mm, ss

    On Error Resume Next

    timeArray = Split(strTime, ":", -1, 1)

    h = timeArray(0): m = timeArray(1): s = timeArray(2)

    If Err Then
        Err.Clear
        Exit Function
    End If

    Do Until s < 60
        s = s - 60
        m = m + 1
    Loop

    Do Until m < 60
        m = m - 60
        h = h + 1
    Loop

    ' As per latest request
    'Do Until h < 24
        'h = h - 24
    'Loop

    If Len(Trim(h)) = 1 Then hh = "0" & h Else hh = h
    If Len(Trim(m)) = 1 Then mm = "0" & m Else mm = m
    If Len(Trim(s)) = 1 Then ss = "0" & s Else ss = s

    Convert = hh & ":" & mm & ":" & ss

    On Error GoTo 0
End Function

HTH

  1. 分割阵列
  2. 使用dateadd()将每个零件添加到空时间
  3. 使用stringbuilder将时间重建为格式正确的字符串

     ' This splits the string in an hours, minutes and seconds part. ' the hours will be in dArr(0), minutes in dArr(1) and seconds in dArr(2) dArr = split("20:72:84", ":") ' Add the hours to an empty date and return it to dt1 dt1 = dateadd("h", dArr(0), empty) ' Add the minutes to dt1. Note: Minutes are noted as "n" and not "m" because the ' "m" is reserved for months. To find out more about the dateadd() please look here: ' http://www.w3schools.com/vbscript/func_dateadd.asp ' When the minutes are bigger than they fit in the date, it automatically falls over to ' next hour. dt1 = dateadd("n", dArr(1), dt1) ' Also add the seconds (the third part of the array) to dt1, also the seconds ' automatically fall over when too large. dt1 = dateadd("s", dArr(2), dt1) ' Now that we created a date, we only have to format it properly. I find it the most easy ' way to do this is with a dotnet stringbuilder, because we can separate code and ' format. The CreateObject creates the stringbuilder. We chain the AppendFormat ' and the ToString methods to it, so actually these are three statements in one. ' Mind the HH in the HH:mm:ss format string, hh means 12 hour notation, HH means 24 ' hour notation. msgbox CreateObject("System.Text.StringBuilder").AppendFormat("{0:HH:mm:ss}", dt1).toString() 

输出21:13:24

编辑:额外的评论,应TS的要求

这似乎显然是一个“ XY问题 ”:OP正在寻求一种解决方法来处理格式错误的数据,而不是首先弄清楚数据是如何格式错误的(并防止它发生)。

20:72:84并不是任何标准的持续时间逻辑表示,任何创建该字符串的方法都是错误的。

从技术上讲 ,根据ISO-8601 (被视为“涵盖与日期和时间相关的数据交换的国际标准”), 持续时间应表示为PnYnMnDTnHnMnS

就是说,我也选择HH:MM:SS ...,但是[显然?]绝对不要显示超过59秒或59分钟,而不是我们的十进制数字(即10...0.8, 0.9, 0.10, 0.11... (大声笑, “零点十一”


确定持续时间

无论如何,这是一个古老的问题,我们不知道您是怎么得到这个奇怪的数字的,但是正如其他人所建议的那样,尽管我更喜欢压缩形式,但我还是会使用Split来解决它:

Function fixWonkyDuration(t As String) As String
    fixWonkyDuration = Format(((Split(t, ":")(0) * 60 + Split(t, ":")(1)) _
        * 60 + Split(t, ":")(2)) / 86400, "HH:mm:ss")
End Function

上面的函数通过将每个部分转换为秒,进行求和,然后临时转换为Date然后使用Format按预期方式显示它,从而固定了“不稳定”的持续时间。

重要的是要注意,输入输出都不是有效的Excel DateTimeDate ),因此两者都声明为String

用法示例:

MsgBox fixWonkyDuration("20:72:84")  'returns "21:13:24"

转换为秒(用于比较,计算等)

顺便说一句,当您具有HH:MM:SS格式的有效持续时间,但想要对其进行计算或比较时,最简单的方法是首先借助TimeValueCDbl将其转换为秒。

最快的方法:

Function DurationToSeconds(d As String) As Long
    DurationToSeconds = CDbl(TimeValue(d)) * 86400#
End Function

用法示例:

MsgBox DurationToSeconds("21:13:24")  'returns 76404

好吧,您可以分为3个变量:h,m和s。 然后检查s> 60。 如果是,则m = m + s / 60和s = s%60。 对于m变量也是如此:

if(m>60) then
    h=h+m/60
    m=m%60

然后,连接h,m和s。

该文本太多,无法发表评论。

当您在任何采用“ General格式的单元格中输入20:72:33 ,它将显示一个序列号。 例如0.883715278

然后,将单元格格式更改为Time 它为您提供了想要查看的格式和数据。

但是

仅当您的秒数below 60 ,以上语句才起作用。 如果输入60, 61, 84 ,'100'等,则无效。

因此,也许你可以卡住它像所有的干扰代码也许你正在使用的是现在。 ;)

它尽可能丑陋。 seconds上执行mod 60 ,然后将单元格格式更改为Time。 或尽可能使用Alex在答案中显示的内容。 这很干净,并且可以数学方式保证您所需的输出。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM