I have a day time format like this "20.10:05:01". It reads as Days, Hours, Minutes, and Seconds. How do I convert that to time span to get the total seconds or hours?
Asked
Active
Viewed 78 times
-1
-
4What's wrong with `TimeSpan.Parse`? – Jeroen Mostert Jul 31 '17 at 12:40
-
https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings – Selman Genç Jul 31 '17 at 12:41
-
Is this a DateTime datatype or is your "20.10:05:01 a string? Cause usually I would say a simple `TimeSpan time = TimeSpan.Parse(string);` would be enough... – Cataklysim Jul 31 '17 at 12:41
-
Check this SO on: [Convert string to TimeSpan](https://stackoverflow.com/questions/24369059/how-to-convert-string-0735-hhmm-to-timespan) – Siva Gopal Jul 31 '17 at 12:42
-
2Possible duplicate of [How to Convert string "07:35" (HH:MM) to TimeSpan](https://stackoverflow.com/questions/24369059/how-to-convert-string-0735-hhmm-to-timespan) – Siva Gopal Jul 31 '17 at 12:45
1 Answers
1
Assuming you have a string
called value
containing such value, you can do
https://msdn.microsoft.com/en-us/library/dd992370(v=vs.110).aspx
TimeSpan timeSpan = TimeSpan.ParseExact(value,@"dd\.hh\.mm\:ss",System.Globalization.CultureInfo.InvariantCulture)
From there, you can use timeSpan.TotalSeconds
or timeSpan.TotalHours
;

Eric Wu
- 908
- 12
- 35