Wrong day count between two date

user2877989
var datetime1 = DateTime.Now;
var datetime2 = DateTime.Now.AddHours(5);
Console.WriteLine((datetime2-datetime1).TotalDays);

datetime1 value is 11:30 PM datetime2 value is a date time value plus more 5 hours.

The console output must be 2. But the result is 0.2xxxxxxxxxx.

I thinks above code calculate days count base on hour of two date. Not based on day of two date.

What should I do to make the output is 2?

vivat pisces

It's not wrong. The TotalDays displays fractional days, and 5 hours (out of 24) is roughly .2 days.

To display the fact that you're dealing with a span of two separate days, ignore the time portion:

Console.WriteLine((datetime2.Date - datetime1.Date).Days + 1);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Count rows for every day between two date columns SQL Server

Get count day between to date Java

Get count day between to date Java

Count data per day between two dates

Get count of day types between two dates

PHP: Two same date statements but day of the week is wrong in one of them

function gives wrong total of a week day between a date range in django

Count occurrence for each day of the week between two dates

Fastest way to get the count of records per day between two dates

Date values between two times for each day of the month

Is there a date of a given day and month between two datetimes in Pandas?

Convert a date between two dates and times to the first day of a month

MSSQL query for all records between two date range of current day

checking a date between two dates gives wrong result

SQL Query to get a count where transaction_date is in between first day of the month and last day of the month

Wrong calculation on date "-1 Day"

Count of rows for every date between a two dates MySQL where no rows

Count days between two date - J2ME

Count times date occurs between two dates in a second table

Count dates between two HTML Date input fields real time

To get the count of working days between two date in linq

Day Date difference between two date calculation in SQL AND C# producing varying result

count on date between

Using jQuery and bootstrap-datetimepicker to get two dates but got wrong day-difference between them

Excel: How to count cells, whose date values represent a day between monday and friday?

How to get each day time in and out and count it in query between date range?

count Sunday in plsql between two dates :date1 and :date2

SimpleDateFormat shows wrong value for day of the extracted date

Date.getDay() javascript returns wrong day