Skip to content Skip to sidebar Skip to footer

How Can I Convert Come String Timespan Variable From Wcf To Hours And Minutes?

I have variable that is come from wcf with http call to javascript is like 'P18DT5H' C#: param.time = new TimeSpan(18, 5, 0, 0); I want to convert hours and minutes? Should I use

Solution 1:

You can use ParseExact:

stringspan="P18DT5H";

IFormatProviderformatProvider= System.Globalization.CultureInfo.InvariantCulture;
TimeSpantimeSpan= TimeSpan.ParseExact(span, "'P'd'DT'h'H'", formatProvider);

inthours= (int)Math.Floor(timeSpan.TotalHours);
intminutes= (int)Math.Round(timeSpan.Subtract(newTimeSpan(hours, 0, 0)).TotalMinutes, 0, MidpointRounding.AwayFromZero);

Console.WriteLine("{0} hours, {1} minutes", hours, minutes);

It will return for your example with no minutes:

437 hours, 0 minutes

Post a Comment for "How Can I Convert Come String Timespan Variable From Wcf To Hours And Minutes?"