Search code examples
sql-serverdatediff

Trying to calculate the number of days since 1/1/1900 and the DateDiff function is off


I am trying to calculate the number of days that have passed between 1/1/1900 and 5/1/2019.

I have tried this using several dates and get the same out come. The value returned is 2 days off.

--
-- calculate the number of days between 1/1/1900 and 5/1/2018
--
SELECT DATEDIFF(DAY,CONVERT(DATE,'1/1/1900'),CONVERT(DATE,'5/1/2018'))

Expected Result: 43221 Actual Result: 43219

Thank you for your help!


Solution

  • DATEDIFF returns the number of days between the two dates. So if you want 1900-01-01 to be numbered as day 1, then you must add 1 to any difference you get from DATEDIFF. In Excel, day 0 is 1899-12-31.

    Secondly, Excel treats 1900 as a leap year, and has a 29-Feb-1900 (day 60 in the Excel numbering system iirc). This was a holdover from Lotus 1-2-3 which originally used a simplified algorithm for leap years (treating every year divisible by 4 as a leap), and remains for backward compatibility

    If you combine these two faults, these account for your off-by-two results.