DateAdd Function

  • Thread starter Thread starter Mel
  • Start date Start date
M

Mel

When I use the DateAdd function using "DateInterval.Weekday" it does
not return the correct date, well at least how I thought the weekday
option should work. It is counting weekends and I only want it to
count the weekdays (Mon-Fri). How can I get it to work? (using
Asp.net 2.0, vb.net)

Example:
dPlanFD = DateAdd(DateInterval.Weekday, 10, "11/04/2008")
'dPlanFD = #11/14/2008# 'I want it to return #11/18/2008#
 
A quick glance at the documentation will tell you what DateInterval.Weekday
actually means:http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.dateint...


Write your own function, something like this:

Function WeekdayDiff(ByVal pdtmDate As DateTime, ByVal pintInterval As
Integer) As DateTime
    Dim intDays As Integer = 1
    Do Until intDays > pintInterval
        pdtmDate = pdtmDate.AddDays(1)
        If pdtmDate.DayOfWeek <> DayOfWeek.Saturday And pdtmDate.DayOfWeek
<> DayOfWeek.Sunday Then
            intDays = intDays + 1
        End If
    Loop
    Return pdtmDate
End Function

And call it like this: Dim dtmEnd As DateTime = WeekdayDiff(DateTime.Now,
10)

Also, do yourself a *HUGE* favour and forget about the Microsoft.VisualBasic
"training wheels" namespace...

That works. I adjusted it a little so it will subtract days too if
the interval is less than zero (I have some tasks that run from Finish
to Start). It works, very cool thank you. Too bad the DateAdd
function won't do it automatically but oh well.

:)
 
Back
Top