简体   繁体   中英

Days difference between c# and SQL Server

I have the following code in c#:

var days = (result.PasswordExpDate.Value - DateTime.UtcNow).Days;

result.PasswordExpDate has a value of 5/25/2019 4:49:37 PM
DateTime.UtcNow has a value of 5/24/2019 4:54:05 PM

It shows days to be a value of 0

In SQL Server I have the following and it shows a value of 1 for Daysdiff

DATEDIFF(day, GetUtcDate(), PasswordExpDate) AS Daysdiff

Wondering why the difference

Because DATEDIFF counts the "boundaries" crossed (Midnights for day ), and TimeSpan.Days counts the number of 24-hour periods wholly contained in the TimeSpan. It's all in the docs:

This function returns the count (as a signed integer value) of the specified datepart boundaries crossed between the specified startdate and enddate.

DATEDIFF

The Days property represents whole days, whereas the TotalDays property represents whole and fractional days.

TimeSpan.Days

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM