Search code examples
sql-servert-sqldatediffleap-year

DATEDIFF and Leap Years for datetimeoffset fields in T-SQL


I use SQL Server 2008 R2. I should calculate difference in days between two datetimeoffset fields, and I did this as:

DATEDIFF(day, CAST(P.Birthdate as datetime), CAST(R.TimeOn as datetime))

What is the best way to calculate the difference including Leap Years? Thank you in advance for any help.


Solution

  • Its already working my friend, since 2016 is a leap year, lets try to check the difference between February 29 2016 and March 01 2016

    select DATEDIFF(day,'2016-02-29','2016-03-01')
    

    If you can see, the result will be 1, meaning DATEDIFF is working with leap year already.