Create a gist now

Instantly share code, notes, and snippets.

SQL Server vs .NET DateTime Leap Year Arithmetic
var leapPrevMinus = new DateTime(2011, 2, 27);
var leapPrev = new DateTime(2011, 2, 28);
var leapPrevPlus = new DateTime(2011, 3, 1);
var leapDayMinusMinus = new DateTime(2012, 2, 27);
var leapDayMinus = new DateTime(2012, 2, 28);
var leapDay = new DateTime(2012, 2, 29);
var leapDayPlus = new DateTime(2012, 3, 1);
var leapNextMinus = new DateTime(2013, 2, 27);
var leapNext = new DateTime(2013, 2, 28);
var leapNextPlus = new DateTime(2013, 3, 1);
Debug.WriteLine(leapPrevMinus.AddDays(365));
Debug.WriteLine(leapPrevMinus.AddYears(1));
Debug.WriteLine(leapPrev.AddDays(365));
Debug.WriteLine(leapPrev.AddYears(1));
Debug.WriteLine(leapPrevPlus.AddDays(365));
Debug.WriteLine(leapPrevPlus.AddYears(1));
Debug.WriteLine(leapDayMinusMinus.AddDays(-365));
Debug.WriteLine(leapDayMinusMinus.AddYears(-1));
Debug.WriteLine(leapDayMinusMinus.AddDays(365));
Debug.WriteLine(leapDayMinusMinus.AddYears(1));
Debug.WriteLine(leapDayMinus.AddDays(-365));
Debug.WriteLine(leapDayMinus.AddYears(-1));
Debug.WriteLine(leapDayMinus.AddDays(365));
Debug.WriteLine(leapDayMinus.AddYears(1));
Debug.WriteLine(leapDay.AddDays(-365));
Debug.WriteLine(leapDay.AddYears(-1));
Debug.WriteLine(leapDay.AddDays(365));
Debug.WriteLine(leapDay.AddYears(1));
Debug.WriteLine(leapDayPlus.AddDays(-365));
Debug.WriteLine(leapDayPlus.AddYears(-1));
Debug.WriteLine(leapDayPlus.AddDays(365));
Debug.WriteLine(leapDayPlus.AddYears(1));
Debug.WriteLine(leapNextMinus.AddDays(-365));
Debug.WriteLine(leapNextMinus.AddYears(-1));
Debug.WriteLine(leapNext.AddDays(-365));
Debug.WriteLine(leapNext.AddYears(-1));
Debug.WriteLine(leapNextPlus.AddDays(-365));
Debug.WriteLine(leapNextPlus.AddYears(-1));
declare @leapPrevMinus Date = '2011-02-27'
declare @leapPrev Date = '2011-02-28'
declare @leapPrevPlus Date = '2011-03-01'
declare @leapDayMinusMinus Date = '2012-02-27'
declare @leapDayMinus Date = '2012-02-28'
declare @leapDay Date = '2012-02-29'
declare @leapDayPlus Date = '2012-03-01'
declare @leapNextMinus Date = '2013-02-27'
declare @leapNext Date = '2013-02-28'
declare @leapNextPlus Date = '2013-03-01'
select
DATEADD(DAY, 365, @leapPrevMinus)
,DATEADD(YEAR, 1, @leapPrevMinus)
,DATEADD(DAY, 365, @leapPrev)
,DATEADD(YEAR, 1, @leapPrev)
,DATEADD(DAY, 365, @leapPrevPlus)
,DATEADD(YEAR, 1, @leapPrevPlus)
,DATEADD(DAY, -365, @leapDayMinusMinus)
,DATEADD(YEAR, -1, @leapDayMinusMinus)
,DATEADD(DAY, 365, @leapDayMinusMinus)
,DATEADD(YEAR, 1, @leapDayMinusMinus)
,DATEADD(DAY, -365, @leapDayMinus)
,DATEADD(YEAR, -1, @leapDayMinus)
,DATEADD(DAY, 365, @leapDayMinus)
,DATEADD(YEAR, 1, @leapDayMinus)
,DATEADD(DAY, -365, @leapDay)
,DATEADD(YEAR, -1, @leapDay)
,DATEADD(DAY, 365, @leapDay)
,DATEADD(YEAR, 1, @leapDay)
,DATEADD(DAY, -365, @leapDayPlus)
,DATEADD(YEAR, -1, @leapDayPlus)
,DATEADD(DAY, 365, @leapDayPlus)
,DATEADD(YEAR, 1, @leapDayPlus)
,DATEADD(DAY, -365, @leapNextMinus)
,DATEADD(YEAR, -1, @leapNextMinus)
,DATEADD(DAY, -365, @leapNext)
,DATEADD(YEAR, -1, @leapNext)
,DATEADD(DAY, -365, @leapNextPlus)
,DATEADD(YEAR, -1, @leapNextPlus)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment