Subtracting Dates in .NET

This may be simple for a lot of .NET developers out there, but for a person like me (coming from classic asp), this was a little tricky.

Problem, I need to know the # of days between 2 dates.

In ASP, I used to use the DateDiff function, and it worked (most of the time). But it’s pretty convoluted and a pain to remember. From just winging it, I’ve come up with this for .NET:

Dim arrThisSplitDate As Array
Dim strThisDate As String = ""
Dim intDaysDifference As Integer

arrThisSplitDate = Split(CDate(strThisPremiumEndDate), "/")

strThisDate = DateSerial(arrThisSplitDate(2), arrThisSplitDate(1), arrThisSplitDate(0))

iintDaysDifference = DateTime.Today.Date.Subtract(strThisDate).Days

This will return you an integer value for the difference in days.

.word.

Discussion Area - Leave a Comment