I'm using .NET framework v 3.5 and i need to parse a string representing a timespan into TimeSpan
object.
The problem is that dot separator is used instead of colon... For example 13.00
, or 22.30
So I'm wondering if I have to replace .
with :
or there is a more clean way to obtain this.
To convert a DateTime to a TimeSpan you should choose a base date/time - e.g. midnight of January 1st, 2000, and subtract it from your DateTime value (and add it when you want to convert back to DateTime ). If you simply want to convert a DateTime to a number you can use the Ticks property.
Split the string into its component parts. Get the number of minutes from the conversion table. Multiply that by the number and that is the number of minutes. Convert that to whatever format you need for the display.
"c" is the default TimeSpan format string; the TimeSpan. ToString() method formats a time interval value by using the "c" format string. TimeSpan also supports the "t" and "T" standard format strings, which are identical in behavior to the "c" standard format string.
Parse out the DateTime
and use its TimeOfDay
property which is a TimeSpan
structure:
string s = "17.34"; var ts = DateTime.ParseExact(s, "HH.mm", CultureInfo.InvariantCulture).TimeOfDay;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With