Format timespan with hours going above 24 instead of using a days part - vb.net

In Visual Basic .NET, I am trying to convert time elapsed in seconds to hh:mm:ss format which can go over 24 hours.
As an example, when try to convert 86400 seconds to a timespan with the following code:
dim sec = 86400
dim res = TimeSpan.FromSeconds(sec).ToString
the output is "1.00:00:00"
while the desired output for me is "24:00:00"
How is it possible to do so?

You just need to do the calculation:
Dim secs = 86400
Dim ts = TimeSpan.FromSeconds(secs)
Dim res = String.Format("{0}:{1:00}:{2:00}", (ts.Days * 24 + ts.Hours), ts.Minutes, ts.Seconds)
Console.WriteLine(res)
Outputs:
24:00:00
If you want at least two digits for the hours, use "{0:00}:{1:00}:{2:00}".
(You could use String.Format("{0}:{1:00}:{2:00}", Math.Floor(ts.TotalHours), ts.Minutes, ts.Seconds) if you think it looks better.)

What you ask isn't available either as a standard or custom format.
You want to emit the total hours as a rounded-down integer followed by the minutes and seconds part. The custom format specifiers only work for specific parts.
You can use the TimeSpan.TotalHours property to retrieve the hours as a double and format the rest of the string using custom specifiers.
Dim ts=TimeSpan.FromDays(1)
String.Format("{0:00}:{1:mm\:ss}",Math.Floor(ts.TotalHours), ts)
Or
Dim ts=TimeSpan.FromSeconds(61)
String.Format("{0:00}:{1:mm\:ss}",Math.Floor(ts.TotalHours), ts)

Related

Values vary after conversion from excel

I am trying to get values from one of the column from excel and i am facing strange issue that i cannot overcome so far.
Excel cells text we working on
Column format is set to: [h]:mm:ss so means hours could exceed 12/24.
When i am getting that values they are in double format as excel probbaly stores it in that way therefore i decided to write function to convert it back again to hours, minutes and seconds so i did that function:
Public Shared Function parseExcelHour(cellInput As String) As String
Dim excelHour As Double = 0
Dim hour As Integer
Dim min As Integer
Dim sec As Integer
Try
excelHour = [Double].Parse(cellInput)
Catch
End Try
sec = CInt((excelHour * 1440 * 60) Mod 60)
min = CInt((excelHour * 1440) Mod 60)
'mod (%) takes only the remainder as an int (if 5/4 = 1.25 , % only takes the number 1 that cannot be divided into an integer)
hour = CInt((excelHour * 1440) / 60)
' with the int cast you get only an integer.
Return hour & ":" & min & ":" & sec
End Function
However when i see the results, they are vary between excel and what i get after conversion. For three of them hours are either -1 or +1 if you compare. Also in one case we have additional + 1 minute. I suppose there is wrong hour calculation but i could be in wrong. See on screeshoot:
Results
Does anyone knows why i got those differences? Is that because i am missing something within my method or something else.
Excel stores a full Datetime equivalent as one double. The part before the decimal point is the days (since 1.1.1900; 1.1.1904 on Mac; note the bug that 1900 is faultily cosnider a leap year in Excel).
The part after is the time of the day, wich is what you apparently want:
https://stackoverflow.com/a/981865/3346583
What you are seeing in excel is meerely a ToString Foramting of the double value. Same way DateTime.ToString() would give you a string representation of whatever value is actually stored (most often a realy big unsigned int, with the ticks since a date).
A difference in full hours sounds like it might be a Timezone issue. But I am not aware that Excel stores Timezones in the first place (or what default timezone it asumes).

How to convert double to minutes in VB

I am currently storing a value as a double but will need to have the value converted to minutes for another function.
For example 1 would convert to 60 (minutes), 3,5 would equal to 210.
I tried timespan but I'm not getting what I am looking for.
TimeSpan.TotalMinutes should give you what you're looking for:
Dim minutes = TimeSpan.FromHours(hours).TotalMinutes

VB 2010 Compare Stopwatch Time

I wan't to compare this two stopwatch times:
01:50:12.543
and
01:50:12.542
How can i do this?
You can get the interval like this:
Dim elapsed as TimeSpan
Dim watch1, watch2 as Stopwatch
...
elapsed = watch1.Elapsed.Subtract(watch2.Elapsed)
If you convert to DateTime and compare, you only get it to the nearest second. You'd have to subtract the milliseconds separately.
You can use a TimeSpan object. First, use TimeSpan.Parse to convert your string values. Then use TimeSpan.Subtract to find the difference, and finally TimeSpan.TotalMilliseconds to tell this difference in milliseconds, the answer will be "1.0" in your case:
Dim ts1 As TimeSpan = TimeSpan.Parse("01:50:12.543")
Dim ts2 As TimeSpan = TimeSpan.Parse("01:50:12.542")
Dim msDiff As Double = ts1.Subtract(ts2).TotalMilliseconds 'answer: 1.0

vb.net Convert string of military time to standard time

So I need to make this to standard time but I cant find an easy way.
Private completedtime1 As String
completedtime1 = oXL.ActiveCell.Offset(0, 7).Value
I need it to take the time and make it HH:mm example 0.001388 which is like 2 mins
If you want to convert from double to Date, you need to use
Dim j as double = DateTime.Now.ToOADate()
Look here
To convert from Date to double, you should use FromOADate()
Dim k as Datetime = DateTime.FromOADate(j)
Look here

Convert HH/MM/SS String To A Number Of Minutes

Could Someone Give Me A Code To Convert
A Label With A String Of 01:34:00
To A Single Number Of Minutes
So 01:34:00 Would Be 94 Mins
Use a TimeSpan object.
Dim ts As TimeSpan
If TimeSpan.TryParse("01:34:00", ts) Then
MessageBox.Show(ts.TotalMinutes.ToString)
End If