davoman
asked on
Convert milliseconds to hh:mm:ss.ms
Hi there I have a integer which is milliseconds representing a lap time
I would like to convert this to the hh:mm:ss.ms format to show the lap time properly using MSSQL or even vb.net
can someone help
I would like to convert this to the hh:mm:ss.ms format to show the lap time properly using MSSQL or even vb.net
can someone help
This is easy, just use a TimeSpan!
Jake
Jake
Dim ts As New TimeSpan(0, 0, 0, 0, 62345)
Label1.Text = ts.ToString()
Create a DateTime value by combining any date (doesn't matter as we don't display it) and add the milliseconds:
Dim t As DateTime = DateTime.Today + TimeSpan.FromMilliseconds( ms)
or
Dim t As DateTime = DateTime.Today.AddMillisec onds(ms)
Now you can format the DateTime value the way that you want:
Dim display As String = t.ToString("HH:mm:ss.fff")
If you convert the value to a datetime value in the database, you loose precision, as drydenhogg mentioned.
If you convert the value to a TimeSpan, you can't control how it's converted to a string. The milliseconds are optional in the conversion, so they will not be displayed using a set number of digits as with the DateTime formatting.
Dim t As DateTime = DateTime.Today + TimeSpan.FromMilliseconds(
or
Dim t As DateTime = DateTime.Today.AddMillisec
Now you can format the DateTime value the way that you want:
Dim display As String = t.ToString("HH:mm:ss.fff")
If you convert the value to a datetime value in the database, you loose precision, as drydenhogg mentioned.
If you convert the value to a TimeSpan, you can't control how it's converted to a string. The milliseconds are optional in the conversion, so they will not be displayed using a set number of digits as with the DateTime formatting.
GreenGhost,
Populating a DateTime value with a TimeSpan added is way less efficient then simply creating a TimeSpan.
It is unnecessary, as what if the race covered 24 hours+ ? TimeSpan.ToString will yield identical results.
Jake
Populating a DateTime value with a TimeSpan added is way less efficient then simply creating a TimeSpan.
It is unnecessary, as what if the race covered 24 hours+ ? TimeSpan.ToString will yield identical results.
Jake
> Populating a DateTime value with a TimeSpan added is way less efficient then simply creating a TimeSpan.
That is almost totally irrelevant, as 98% of the time is used to convert the value to a string anyway.
> TimeSpan.ToString will yield identical results.
Not at all.
With a DateTime value you can format it into a string that always has the same number of digits for fractional seconds. With a TimeSpan value that is not possible.
That is almost totally irrelevant, as 98% of the time is used to convert the value to a string anyway.
> TimeSpan.ToString will yield identical results.
Not at all.
With a DateTime value you can format it into a string that always has the same number of digits for fractional seconds. With a TimeSpan value that is not possible.
ASKER
I am going to have to be able to deal with more that 2 hours ie a time could be
hh:mm:ss
25:03:12
So I need a method that will allow for this
hh:mm:ss
25:03:12
So I need a method that will allow for this
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
GreenGhost,
I see what you mean how milliseconds aren't included if the number is a whole second.
Seems DateTime is a better way, sorry.
Jake
I see what you mean how milliseconds aren't included if the number is a whole second.
Seems DateTime is a better way, sorry.
Jake
ASKER
greenghost, that looks good and will still not be restricted by the 24 hour clock is that correct
That is correct. The TotalHours property returns the TimeSpan value as hours, so if the TimeSpan contains 25:03:12, the TotalHours returns the value 25.053333333333333333. The Math.Floor method is used to round the value down to 25.0 to get only the hours.
ASKER
actuall the 25:03:12 would be fine actually I won't need the floor, it will allow them to see their totalrace time
thanks I will check this and post acceptence
thanks I will check this and post acceptence
Yes, but the minutes and seconds is created by formatting the DateTime value. If you don't use Math.Floor, you would get 25.0533333333333333333:03: 12.
ASKER
Sorry for the delay thanks a lot for the answer
Select DateAdd(ms,12345,0)
where 12345 is your number of ms, and 0 is considered the start date for SQL, (1/1/1900). Assuming no lap time exceeds a single day, discard the date part and display only the time.
One caveat is that times stored in SQL are not accurate to the MS, the accuracy is to 3.3 ms. 1 millisecond accuracy is not achievable using the datetime type.
hth.