c# - How to get datetime difference in double value using Timespan? -


i want save datetime difference in sqlserver database double value using timespan hour. want hh.mm format.

my code follows :

datetime starttime = convert.todatetime(date1); datetime endtime = convert.todatetime(date2); timespan span = endtime.subtract(starttime); double timedeff=span.hours; //here getting hour.i want in 'hh.mm' format(double) 

i assume want in floating point format, e.g. xx.yy yy = 0..60 right? 10.02 means 10:02.

the solution easy:

datetime starttime = convert.todatetime(date1); timespan diff = convert.todatetime(date2).subtract(starttime);  double timedef = diff.hours + diff.minutes / 100.0; 

otherwise do

double timedef = diff.hours + diff.minutes / 60.0; 

i imagine can have use if this:

var str = timedef.tostring("0.00");  

... there better ways that. timespan , datetime lot of magic, if don't need double, stick until end.

a note fixed point

datetime uses integers math. double's use floating point. floating point arithmetic definition introduces errors; therefore it's better (and safer) use integer arithmetics.

datetime , timespan provide date , delta-time operations.


Comments