DateAdd Function

Discussion in 'ASP .Net' started by Mel, Nov 4, 2008.

  1. Mel

    Mel Guest

    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#
    Mel, Nov 4, 2008
    #1
    1. Advertising

  2. Mel

    Mel Guest

    On Nov 4, 11:49 am, "Mark Rae [MVP]" <> wrote:
    > "Mel" <> wrote in message
    >
    > news:...
    >
    > > 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.

    >
    > A quick glance at the documentation will tell you what DateInterval.Weekday
    > actually means:http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.dateint...
    >
    > > 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)

    >
    > 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...
    >
    > --
    > Mark Rae
    > ASP.NET MVPhttp://www.markrae.net


    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.

    :)
    Mel, Nov 7, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt Tapia

    DateAdd Function and error BC30451

    Matt Tapia, Aug 13, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    2,176
    Chris R. Timmons
    Aug 13, 2003
  2. lofty

    DateAdd does not work

    lofty, Jul 20, 2003, in forum: ASP General
    Replies:
    5
    Views:
    172
    Chris Barber
    Jul 20, 2003
  3. Tipple
    Replies:
    2
    Views:
    124
    Tipple
    Oct 16, 2003
  4. Øyvind Isaksen

    dateadd()

    Øyvind Isaksen, Oct 17, 2003, in forum: ASP General
    Replies:
    4
    Views:
    133
    Ken Schaefer
    Oct 18, 2003
  5. Gabe

    Dateadd function not adding up...

    Gabe, Oct 30, 2003, in forum: ASP General
    Replies:
    3
    Views:
    147
    Ray at
    Oct 31, 2003
Loading...

Share This Page