DateDiff – Subtract

In my VB.Net code I’ve always used the DateDiff function to determine the number of days between 2 dates. Today I was playing around working out which order to put dates in and discovered that the Date object as a method called Subtract. This returns a TimeSpan then its just a case of using the Days property

startDate.Subtract(now).Days

Note that if startDate is before todays date this will return a negative value

>? now.subtract(cdate("1 Jan 2009")).days
1
>? cdate("1 Jan 2009").subtract(now).days
-1
>? now.subtract(cdate("1 Jan 2008")).days
367


Leave a Reply