-2

Following code return 30 days instead of 31 days.

DateTime dtFromDate   = Convert.ToDateTime("2015-01-01");
DateTime dtToDateDate = Convert.ToDateTime("2015-01-31");     
NoOfDays  =  (int) (dtToDateDate- dtFromDate).TotalDays;

and the following code return 31 days.

NoOfDays = (int)DateTime.DaysInMonth(2015,1);   

How to calculate exact no of days between two dates?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Abrar Ahmad
  • 145
  • 2
  • 7
  • 3
    How could that return 31 days? Does 31 -1 equals 31 ? – Rohit Prakash Jan 29 '15 at 05:39
  • Duplicate of [Calculate difference between two dates (number of days)?](http://stackoverflow.com/questions/1607336/calculate-difference-between-two-dates-number-of-days), kindly make sure to read the comments on the mentioned duplicate. – Prix Jan 29 '15 at 05:40
  • FYI, this is not a "built in C# function". It's a .NET Framework method on the .NET Framework `System.DateTime` type. – John Saunders Jan 29 '15 at 05:41
  • Rohit why 31-1, what if we want to calculate no of days in a month we subtract first date to last date? and how the DaysInMonth Calculate no of days – Abrar Ahmad Jan 29 '15 at 05:44
  • 2
    @AbrarAhmad, its simple logic, if you have to count number of days from 1st to 31st, then you complete your one day at 2nd date, two day at 3rd date and so 30th on 31st date. – Rohit Prakash Jan 29 '15 at 05:48
  • If you want to count the number of whole numbers between 1 and 10 you don't subtract 10 from 1 (10 - 1 = 9). – Aron Jan 29 '15 at 06:04

3 Answers3

1

in the first one you omitted all days at and before "2015-01-01" from the date "2015-01-31". which is logically 30 (2015/1/31 included)

in the second you are just asking number of days in January which is 31

Nothing is not normal here

chouaib
  • 2,763
  • 5
  • 20
  • 35
1

That's because the day starts from 00:00:00 hour (midnight), so for 31/1 so it will not be included, but if you added the hours you will get the valid number of days. Or you can change the enddate to be "1/2/2015 00:00:00" and you will get the valid numbers too.

Omar.Alani
  • 4,050
  • 2
  • 20
  • 31
1

I know this is late but I want to write more explanation if you guys let me!

Let's look at your examples line by line;

DateTime dtFromDate   = Convert.ToDateTime("2015-01-01");
DateTime dtToDateDate = Convert.ToDateTime("2015-01-31");   

With this conversation, your dtFromDate will be 01/01/2015 00:00:00 and dtToDateDate will be 31/01/2015 00:00:00 since you didn't write any time part, it will be assigned to midnight by default.

With dtToDateDate- dtFromDate line, you will get a TimeSpan which is exactly 30 day long as {30.00:00:00}. Why? Well, simple;

+---------------------+---------------------+----------------+
|      FirstDate      |      LastDate       | Day Difference |
+---------------------+---------------------+----------------+
| 01/01/2015 00:00:00 | 01/01/2015 00:00:00 | 0              |
| 01/01/2015 00:00:00 | 02/01/2015 00:00:00 | 1              |
| 01/01/2015 00:00:00 | 03/01/2015 00:00:00 | 2              |
| ...                 | ...                 | ...            |
| ...                 | ...                 | ...            |
| 01/01/2015 00:00:00 | 31/01/2015 00:00:00 | 30             |
+---------------------+---------------------+----------------+

But how about DateTime.DaysInMonth(2015, 1)?

DaysInMonth method returns the number of days in the specified month and year. Since October has 31 days in Gregorian Calender, it returns 31.

From wikipedia;

October is the tenth month of the year in the Julian and Gregorian Calendars and one of seven months with a length of 31 days.

But this method doesn't use any DateTime difference to calculate them. Here how it's implemented:

public static int DaysInMonth(int year, int month)
{
    // IsLeapYear checks the year argument
    int[] days = IsLeapYear(year)? DaysToMonth366: DaysToMonth365;
    return days[month] - days[month - 1];
}

Since 2015 is not a leap year, days array will be equal to DaysToMonth365 which is defined as;

private static readonly int[] DaysToMonth365 =
{
   0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365
};

And we provided month parameter as 1, this method returns

days[1] - days[0]

which is equal to

31 - 0

which is equal to 31.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364