0

I want get diffrences Day,Hour and Day between two days.

I use belowe Code :

DateTime LastDate = DateTime.Parse("2/12/2015 11:24:23 AM");
int differenceDay = DateTime.Now.Subtract(LastDate).Days;
int differenceHoure = DateTime.Now.Hour - LastDate.Hour;//returns -11
int differenceMinute = DateTime.Now.Minute - LastDate.Minute;

When I want get Hours its return mines (-11 e.t).

How can I get positive Diffrence Hour ?

anyone can you help me? I want get Last Dat and show its by string how days afterd now.

job 1393
  • 21
  • 1
  • 7
  • Next time, please use Google first instead of directly asking a question here. You _may_ find your answer in 30 seconds. – Soner Gönül Feb 12 '15 at 08:40
  • @sloth: No, that's *not* a duplicate. That's asking for the difference *just* in hours. This is asking for "days, hours and minutes". – Jon Skeet Feb 12 '15 at 08:43
  • @JonSkeet This would be a better duplicate IMO http://stackoverflow.com/questions/10538507/days-hours-minutes-seconds-between-two-dates – Soner Gönül Feb 12 '15 at 08:46
  • @SonerGönül: Yes, that would be reasonable - although it wouldn't explain what was wrong with the OP's current approach. – Jon Skeet Feb 12 '15 at 08:46
  • @SonerGönül @ JonSkeet I agree that's a better duplicate. – sloth Feb 12 '15 at 08:48

1 Answers1

7

You're subtracting component-wise (i.e. "this hour-of-day minus that hour-of-day, this minute-of-hour minus that minute-of-hour"). Don't do that - it won't work if the current hour-of-day is earlier than the hour-of-day of lastDate, or the same for minute-of-hour - you get a negative value, exactly as you've seen.

Instead, subtract one DateTime from another to get a TimeSpan and use that single TimeSpan for all the components:

DateTime lastDate = DateTime.Parse("2/12/2015 11:24:23 AM");
TimeSpan difference = DateTime.Now - lastDate;
int days = difference.Days;
int hours = difference.Hours;
int minutes = difference.Minutes;

That will still be negative if lastDate is after DateTime.Now, of course.

Note that this will give you a result which is meaningful if you display all three components. For example, it might give you "2 days, 3 hours and 10 minutes". If instead you want to represent the same TimeSpan as "2.194 days" or "51.166 hours" or "3160 minutes" then you can use TotalDays, TotalHours and TotalMinutes.

If you always want a positive TimeSpan - the equivalent of Math.Abs but for TimeSpan you can just use TimeSpan.Duration():

TimeSpan difference = (DateTime.Now - lastDate).Duration();
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • this line `int hours = difference.Hours;` ites return **-10** How can I get posetive number for example I want get **10** instedof **-10** – job 1393 Feb 12 '15 at 08:46
  • @job1393: Why would you want 10? The difference *is* -10 hours. If you want to just always have a positive TimeSpan and ignore which is earlier, you can just invert the `TimeSpan` - I'll edit to show that. – Jon Skeet Feb 12 '15 at 08:48
  • 3
    You can also use the [`TimeSpan.Duration`](https://msdn.microsoft.com/en-us/library/system.timespan.duration%28v=vs.110%29.aspx) method to get the absolute value. – Tim Schmelter Feb 12 '15 at 08:50
  • last hour is **11 am** and now is **00 pm** it sould be returns **one hour insted of **-10** – job 1393 Feb 12 '15 at 08:50
  • @job1393: By "00pm" do you mean midday? It doesn't help that we don't know what time zone you're in. Are you saying it's currently 2015-02-12T12:00:00? If so, I'd expect the result to just be 36 minutes. I suspect your problem is really a time zone one. – Jon Skeet Feb 12 '15 at 08:52
  • 1
    @TimSchmelter: Nice - I'd never seen that before! (Which surprises me, given the amount of time I've spent with the date/time APIs...) – Jon Skeet Feb 12 '15 at 08:52