2

I am trying to find days between two dates using a Time span.

nextdate1='2014-12-20'

today    `='2014-12-18'`

My sample code is:

DateTime nexdate1 = dr.GetDateTime(2); //gets from database. I checked and the value is correct
DateTime today = DateTime.Now;
TimeSpan nextdate = nexdate1.Subtract(today);
int difference = nextdate.Days;

Now I get difference=1. Actually the difference is 2 (20-18).

Why it shows difference as 1?

Phoenix
  • 1,045
  • 1
  • 14
  • 22
Semil Sebastian
  • 111
  • 1
  • 5
  • 18
  • 1
    What are the "time" parts of the dates? Is it saying 1 day 23 hours for example? – Aron Dec 18 '14 at 05:44
  • 1
    You're subtracting the entire DateTimes from each other instead of just the days (Note that sth. like 47 hrs will give you one day.) Try using dr.GetDateTime(2).Date and DateTime.Now.Date and see if that makes a difference. – Thomas Weller Dec 18 '14 at 05:45
  • time zones ?? "The Subtract(DateTime) method does not consider the value of the Kind property of the two DateTime values when performing the subtraction. Before subtracting DateTime objects, ensure that the objects represent times in the same time zone. Otherwise, the result will include the difference between time zones." – Kavindu Dodanduwa Dec 18 '14 at 05:45
  • found this useful - http://stackoverflow.com/questions/10871755/subtracting-two-dates – Kavindu Dodanduwa Dec 18 '14 at 05:47
  • @Aron, it shows only 1, no hours part and all – Semil Sebastian Dec 18 '14 at 05:47
  • @SaraJohn Try looking at the `nextdate` object, not the `difference` variable – Asad Saeeduddin Dec 18 '14 at 05:48
  • @Asad, Your last deleted comment works for me. – Semil Sebastian Dec 18 '14 at 05:52
  • Change the next part in the Code: nexdate1.Subtract(today); to nexdate1.Date.Subtract(today.Date); The Date property set the times to 00:00:00 – Eduardo Vélez Dec 18 '14 at 05:53

2 Answers2

4

TimeSpan.Days is an int. Your answer is getting truncated.

In the following code:

var date1 = DateTime.Parse("Dec 12, 2014");
var date2 = DateTime.Parse("Dec 14, 2014");
var difference = (date2 - date1).Days;

difference is set to 2.

But in this code:

var date1 = DateTime.Parse("Dec 12, 2014 12:01 AM");
var date2 = DateTime.Parse("Dec 14, 2014");

var difference = (date2 - date1).Days;

difference is set to 1.

When we look at the timespan date2 - date1, we get the following:

{1.23:59:00}
    Days: 1
    Hours: 23
    Minutes: 59
    Seconds: 0
    TotalDays: 1.9993055555555554

You should set nextDate = nexdate1.Date.Subtract(DateTime.Today); so that you're only looking at the difference in days, or take (int)Math.Round(nextDate.TotalDays)

Tim Cooke
  • 862
  • 7
  • 14
1

Actually your datetime variable having time part, because of that time your result get affected. Hence you have to find only date part of your datetime value:

Use DateTime.Date

Do it like this:

DateTime nexdate1 = dr.GetDateTime(2);
DateTime today = DateTime.Now;
TimeSpan nextdate = nexdate1.Date.Subtract(today.Date); // Here find only date part from datetime value.
//TimeSpan nextdate = nexdate1-today
int difference = nextdate.Days;
Ankush Madankar
  • 3,689
  • 4
  • 40
  • 74