TimeSpan Conversion for Swimming

How to convert TimeSpan to float, taking into account the whole processor (hour minute), for example, if (unit = hour) convert TimeSpan to floating hours

In another context, is there a "Timespan" data type in SQL Server?

+4
source share
5 answers

Use the Total* properties on TimeSpan , for example. TimeSpan.TotalHours .

+4
source

Example minutes:

 var t = new TimeSpan(); var total = t.TotalMinutes; 
0
source

You can do something like this

 TimeSpan elapsedTime = new TimeSpan(125000); float floatTimeSpan; int seconds, milliseconds; seconds = elapsedTime.Seconds; milliseconds = elapsedTime.Milliseconds; floatTimeSpan = (float)seconds + ((float)milliseconds / 1000); Console.WriteLine("Time Span: {0}", floatTimeSpan); 

The output of the program is as follows:

 Time Span: 0.012 
0
source
 internal static string TimeSpanToDouble(TimeSpan timeSpan, string unit) { double result = 0; if (unit.Equals("MINUTES")) result = timeSpan.TotalMinutes; else if (unit.Equals("HOURS")) result = timeSpan.TotalHours; else if (unit.Equals("DAYS")) result = timeSpan.TotalHours / 24; else throw new Exception(); return Convert.ToString(result); } 
0
source

You can use Convert.ToSingle , for example:

 var ts = new Timespan(0, 1, 1, 30); var minutes = Convert.ToSingle(ts.TotalMinutes); var hours = Convert.ToSingle(ts.TotalHours); 

The resulting minutes and hours will be 61.5 and 1.025 respectively

Check out this dotnetfiddle: https://dotnetfiddle.net/uSQfk0

0
source

All Articles