Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse string in HH.mm format to TimeSpan

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.

like image 350
davioooh Avatar asked Sep 19 '12 10:09

davioooh


People also ask

How do I convert DateTime to TimeSpan?

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.

How do I convert string to minutes?

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.

What format is TimeSpan?

"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.


1 Answers

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; 
like image 106
Ivan Golović Avatar answered Sep 20 '22 04:09

Ivan Golović