Due dates calculation with SQL or in C#


Based on invoice date to decide when the due date is. If we decide a business rule for this is: take 25 as a cutoff date.
Here is a solution in SQL:

SELECT Vendorname, Invoicedate,

CASE  WHEN Day(Invoicedate) <= 25 THEN Dateadd(DAY, 9, Dateadd(Mm, Datediff(M, 0, Invoicedate) + 1, 0))

ELSE Dateadd(DAY, 9, Dateadd(Mm, Datediff(M, 0, Invoicedate) + 2, 0))

END AS Paydate FROM @t

 
 

We can work with datetime in C# to get another solution:

DateTime dt = new DateTime(2009, 12, 21);

//DateTime dt = new DateTime(2009, 10, 21);

if (dt.Day>25)

{

if (dt.Month>10)

{

DateTime Paydate = new DateTime(dt.Year+1, (dt.Month+2)%12, 10);

Response.Write(Paydate.ToString());

}

else

{

DateTime Paydate = new DateTime(dt.Year, dt.Month + 2, 10);

Response.Write(Paydate.ToString());

}

}

else

{

if (dt.Month > 11)

{

DateTime Paydate = new DateTime(dt.Year + 1, (dt.Month + 1) % 12, 10);

Response.Write(Paydate.ToString());

}

else

{

DateTime Paydate = new DateTime(dt.Year, dt.Month + 1, 10);

Response.Write(Paydate.ToString());

}

}

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s