0

I want to save DateTime difference in sqlserver database as a double value but using Timespan I get only hour. I want to get it hh.mm format.

My code is as follows :

DateTime starttime = Convert.ToDateTime(date1);
DateTime endtime = Convert.ToDateTime(date2);
TimeSpan span = endtime.Subtract(starttime);
double timedeff=span.Hours;
//Here I getting only hour.I want to this in 'hh.mm' format(double)
Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112
Safeena
  • 395
  • 2
  • 7
  • 21
  • 1
    `hh.mm` is not a `double` format. It is a formatted `TimeSpan`. Can you please example of `starttime` and `endtime` with expected output? – Soner Gönül Jun 03 '15 at 06:07
  • possible duplicate of [How to get the unix timestamp in C#](http://stackoverflow.com/questions/17632584/how-to-get-the-unix-timestamp-in-c-sharp) – ckruczek Jun 03 '15 at 06:18
  • 1
    How could this be related @ckruczek? – Rohit Vipin Mathews Jun 03 '15 at 06:43
  • @Rohit: I thought this question is coming. I was just thinking of the difference he needs. And I think, a unix timestamp difference is more accurate than a float number. – ckruczek Jun 03 '15 at 06:44
  • 1
    I dont hink you can judge like that, without know the exact requirements or reasons behind OPs scenario. @ckruczek there is also No reason for OP to use UNIX timestamp difference instead of DateTime difference. – Rohit Vipin Mathews Jun 03 '15 at 06:49

3 Answers3

1

I assume you want it in floating point format, e.g. xx.yy where yy = 0..60 right? So you get 10.02 which means 10:02.

The solution is easy:

DateTime starttime = Convert.ToDateTime(date1);
TimeSpan diff = Convert.ToDateTime(date2).Subtract(starttime);

double timedef = diff.Hours + diff.Minutes / 100.0;

Otherwise this will do

double timedef = diff.Hours + diff.Minutes / 60.0;

I imagine it can have some use if you do something like this:

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

... but there are better ways to do that. TimeSpan and DateTime do a lot of magic, if you don't really need a double, stick with it until the very end.

A note about fixed point

DateTime uses integers to do its math. Double's use floating point. Floating point arithmetic by definition introduces errors; therefore it's probably better (and safer) to use integer arithmetics.

DateTime and Timespan provide exactly this for date and delta-time operations.

atlaste
  • 30,418
  • 3
  • 57
  • 87
0

Assuming you wanted the difference to be in hours, convert minutes and seconds also into hours.

double timediff=span.Hours + span.Minutes/60.0 + span.Seconds/(60.0*60.0);
elimad
  • 1,132
  • 1
  • 15
  • 27
  • 1
    Why not just do span.TotalSeconds() * 3600 ? Then you also get the millis etc? PS: there's also an error in your (integer) division – atlaste Jun 03 '15 at 06:05
  • @atlaste: sorry, for the division error. thanks. Thanks also for `TotalSeconds()`: I didn't know about it. – elimad Jun 03 '15 at 06:16
0

I'm not really sure about what do you mean by

'hh.mm' format(double)

Assuming it to be something like :

9hrs 10 minutes = 9.10

   DateTime starttime = Convert.ToDateTime(date1);
   DateTime endtime = Convert.ToDateTime(date2);
   TimeSpan span = endtime.Subtract(starttime);
   var datetime = new DateTime(span.Ticks).ToString("H:mm");
   string datetimestring = datetime.ToString().Replace(":",".");
   double timeinDouble =  double.Parse(datetimestring, CultureInfo.InvariantCulture);

Here is a Sample Project if you want to test different scenarios - LINK

Note :

  1. Be sure to use a "H", not "h", as a format specifier, otherwise an hours value of 0 might be printed out as "12".

  2. If an elapsed time is e.g. 26 hours long, it prints out as "02:00" hours.

  3. This method will cause problems if the timespan is negative.

Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112
  • It's incorrect; different locales give different results. – atlaste Jun 03 '15 at 06:07
  • @atlaste - Could please expand? – Rohit Vipin Mathews Jun 03 '15 at 06:18
  • 2
    `double.Parse("12.12")` -- in the Netherlands this gives 1212, in the UK this gives 12.12. A separator token is part ot the localization / cultureinfo; as such you should either not use it for literal representations or fix it manually (e.g. using an explicit CultureInfo). Once you do that, your implementation will be correct - but also pretty bad performance-wise. – atlaste Jun 03 '15 at 06:20
  • A very valid point. i was thinking on localization issues related to Time. – Rohit Vipin Mathews Jun 03 '15 at 06:22