-2

I trying to get Number between 2 dates:

DateTime base;
....
base = DateTime.ParseExact(pos[13], "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
//example base =  2014-06-21 17:00:00

DateTime col_N;

//then some for loop
col_N = DateTime.ParseExact(pos[k], "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture); 
//example col_N = 2014-06-22 00:00:00

To get days between I'm doing like below:

int date_diff = (col_N - base).Days;

but it return me 0.

Also, when I checked:

string diff_dat2 = (col_N - base).TotalDays.ToString();

I got 0,291666666666667.

How to correct it to get 1 day ?

4est
  • 3,010
  • 6
  • 41
  • 63
  • 1
    It's unclear what you're asking. Do you want to just round the number up to 1? 7/24 of a day is the correct answer in this example so what do you want to correct? – V0ldek Jul 10 '19 at 10:30
  • what should happen if the difference between the dates is just 1 second? Should it still come back as 1 day? Or do you just want the difference between the `date` component of `datetime`? – NoviceProgrammer Jul 10 '19 at 10:44

1 Answers1

3

I am not sure what exactly you are trying to achieve but I assume you want to get difference in days in dates only excluding the time.

To achieve this you need to remove the time component of DateTime by doing .Date:

int date_diff = (col_N.Date - base.Date).Days;

TotalDays is correct in your example as there is no full date between the two times that you have, but if you operate only in dates, you will get your answer just right.

Ilya Chernomordik
  • 27,817
  • 27
  • 121
  • 207