时间格式的公式,如果最后两位小数达到60,它如何转换,它将转换为左字符。

问题描述:

样品运行:

TotalTime.Text = 3.56

time.Text = 5.07 03/15/2014





MyResult = 8.63 03/15/2014

MyExpectedResult = 9.03 03/15/2014



这是我的代码:

Sample run:
TotalTime.Text = 3.56
time.Text = 5.07 03/15/2014


MyResult = 8.63 03/15/2014
MyExpectedResult = 9.03 03/15/2014

This is my code:

TextBox7.Text = Format(Val(TotalTime.Text) + Val(Strings.Left(time.Text.Trim, 5)), "##.00")

                If Val(TotalTime.Text.Trim.Split(".")(1)) >= 60 Then
                    TotalTime.Text = Val(TotalTime.Text.Trim.Split(".")(0)) + 1 & "." & Val(TotalTime.Text.Trim.Split(".")(1) - 60)
                End If

                TextBox7.Text = Format(Val(TotalTime.Text) + Val(Strings.Left(time.Text.Trim, 5)), "##.00") & Strings.Right(time.Text.Trim, 11)

TextBox7.Text = Format(Val(TextBox6.Text) + Val(Strings.Left(time.Text.Trim, 5)), "##.00")

                If Val(TextBox7.Text.Trim.Split(".")(1)) >= 60 Then
                    TextBox7.Text = Val(TextBox7.Text.Trim.Split(".")(0)) + 1 & "." & Val(TextBox7.Text.Trim.Split(".")(1) - 60) & Strings.Right(time.Text.Trim, 11)
                Else
                    TextBox7.Text = Format(Val(TextBox6.Text) + Val(Strings.Left(time.Text.Trim, 5)), "##.00") & Strings.Right(time.Text.Trim, 11)
                End If




)


您需要将日期和时间视为AS日期和时间。

使用Date和TimeSpan结构来处理它。

You need to treat the date and time AS date and time.
Use the Date and TimeSpan structures to handle it.
Dim baseTime as Date
Dim totalTime as TimeSpan
baseTime = Date.ParseExact(time.Text, "H.mm MM/dd/yyyy", CultureInfo.InvariantCulture)
totalTime = Date.TryParseExact(TotalTime.Text, "H.mm", CultureInfo.InvariantCulture).TimeOfDay

TextBox7.Text = (baseTime + totalTime).ToString("H.mm MM/dd/yyyy", CultureInfo.InvariantCulture)





(警告:我在c#中对此进行了测试并将其转换为VB,但我的VB生锈了。可能需要调整。)



(Caveat: I tested this in c# and converted it to VB, but my VB is rusty. It may need tweaking.)