Answered by:
convert from timespan to integer

Question
-
User-1400984835 posted
I need to convert a timespan into an integer. I have two dates that I am subtracting one from the other, returning a value in days. I then need to perform some arithmatical functions on that value but I cant figure out how. Any advice? Thanks, will mark Answered for any feedback that leads to a solution.
Friday, October 29, 2010 4:49 PM
Answers
-
User2117486576 posted
I think these two expressions are the same value:
Convert.ToInt32(TimeSpan.TotalDays)
TimeSpan.Days
Converting TimeSpan.TotalDays to int would just leave off the fractional days. If all you want is the number of days in the timespan then you don't need to convert - just use the Days property.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, October 29, 2010 5:53 PM
All replies
-
User2117486576 posted
I think that it depends on what unit of measure you want. For intance if you want the number of ticks then TimeSpan.Ticks will return the number of ticks. If you want the value in seconds I think this will work:
int seconds = (((ts.Days * 24) * 3600) + (ts.Hours * 3600) + (ts.Minutes * 60) + (ts.Seconds));
Friday, October 29, 2010 5:17 PM -
User-431565850 posted
Convert.ToInt32(myTimeSpan.TotalDays)
Friday, October 29, 2010 5:18 PM -
User2117486576 posted
I think these two expressions are the same value:
Convert.ToInt32(TimeSpan.TotalDays)
TimeSpan.Days
Converting TimeSpan.TotalDays to int would just leave off the fractional days. If all you want is the number of days in the timespan then you don't need to convert - just use the Days property.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, October 29, 2010 5:53 PM -
User370212504 posted
Hi,
follow this :
Dim TimeSpan As TimeSpan = New TimeSpan(DateTime.UtcNow.AddHours(4.5).Ticks) Dim Today As Integer = Val(Math.Round(CDbl(TimeSpan.TotalDays)))
Friday, October 29, 2010 6:06 PM -
User-1400984835 posted
Thank you. Concise solution
Friday, October 29, 2010 9:17 PM