Reputation: 3549
Trying to figure out time formatting is absolutely killing me.
When I press a button I collect a start time using:
StartTime = DateTime.Now.ToString("hh.mm.ss.tt")
Then when the calculation is done I have:
EndTime = DateTime.Now.ToString("hh.mm.ss.tt")
What I would like to do is to:
TotalTime = EndTime - StartTime
But to print the time using the format; "?# Hours, ## Minutes, & ## Seconds"
Upvotes: 2
Views: 10665
Reputation: 38757
Solution by OP.
LarsTech answer got me thinking about a better solution. I completely forgot that I had datetime.now.tostring. What I ended up going is:
timenow = DateTime.Now
start = DateTime.Now
Dim totaltime As TimeSpan = (timenow - start)
xlWorkSheet201.Cells(3, 9) = "Total Test Time: " & Format(totaltime.Hours, "#0") & " Hours, " & Format(totaltime.Minutes, "#0") & "Minutes, & " & Format(totaltime.Seconds, "00") & "Seconds."
Upvotes: 0
Reputation: 128447
Dim TotalTime As TimeSpan = EndTime - StartTime
Console.WriteLine( _
"{0:#0} Hours, {1:00} Minutes, & {2:00} Seconds", _
Math.Truncate(TotalTime.TotalHours), _
TotalTime.Minutes, _
TotalTime.Seconds _
)
Upvotes: 1
Reputation: 81675
I probably wouldn't use the ToString()
function on the StartTime and EndTime variables, since that turns them into strings.
Try this:
Dim StartTime as DateTime = DateTime.Now
'' Do Stuff
Dim EndTime As DateTime = DateTime.Now
Dim TotalTime As TimeSpan = EndTime - StartTime
Me.Text = TotalTime.ToString("hh':'mm':'ss")
Of course, you can skip the TotalTime variable and just calculate it in place:
Me.Text = (EndTime - StartTime).ToString("hh':'mm':'ss")
Upvotes: 7