Consulting

Results 1 to 5 of 5

Thread: Add a day to a date

  1. #1

    Cool Add a day to a date

    Hi,

    I'm using the below code to add the value in TextBox3 to the value in TextBox10, and put it in TextBox12.

    TextBox3 is a date and TextBox10 is a number, in this instance a 1.

    This works fine, but I want to make a slight change so that it adds exactly 1 day the date in TB10, i.e. if TB10 is Now(), so it shows the date and time, I want TB12 to show tomorrows date, with the same time, so exactly 24 hours on.

    Any ideas if this is possible?

     
    TextBox3.Value = Format(Now, "d-mmmm-yy h:mm")
    TextBox10.Value = 1
    TextBox12.Value = ""
    newDate = Format(DateValue(TextBox3.Value) + TextBox10.Value, "d-mmmm-yy")
    If Weekday(newDate, vbUseSystemDayOfWeek) = 6 Then
        newDate1 = Format(DateValue(newDate) + 2, "d-mmmm-yy h:mm")
    Else
        If Weekday(newDate, vbUseSystemDayOfWeek) = 7 Then
             newDate1 = Format(DateValue(newDate) + 1, "d-mmmm-yy h:mm")
        Else
             newDate1 = Format(DateValue(newDate), "d-mmmm-yy h:mm")
        End If
     End If
    If TextBox12.Value = "" Then
        TextBox12.Value = Format(DateValue(newDate1), "d-mmmm-yy h:mm")
    End If
    We're a Kingdom, and we're United!!

  2. #2
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    You'll want to use the DateAdd function.
    e.g:
    'now plus one day
    MsgBox DateAdd("d", 1, Now())
    'now plus one day, formatted
    MsgBox Format(DateAdd("d", 1, Now()), "d-mmmm-yy h:mm")
    K :-)

  3. #3

    Cool

    Many thanks....worked a treat.

    However, I want to add either 1 or 2 days onto the original newly calulated date, dependant on if the new date falls on a Saturday or Sunday.

    I've tried some codes (below) but can't seem to make it work!

    Attempt 1:
     
    newDate = Format(DateAdd("d", TextBox10.Value, Now()), "d-mmmm-yy h:mm")
    Select Case Weekday(newDate, vbUseSystemDayOfWeek)
        Case 6
            newDate1 = Format(DateValue(DateAdd("d", 2, newDate)), "d-mmmm-yy h:mm")
        Case Else
            newDate1 = Format(DateValue(newDate), "d-mmmm-yy h:mm")
    End Select
    Attempt 2:
     
    newDate = Format(DateAdd("d", TextBox10.Value, Now()), "d-mmmm-yy h:mm")
    Select Case Weekday(newDate, vbUseSystemDayOfWeek)
        Case 6
            newDate1 = DateAdd("d", 2, newDate)
        Case Else
            newDate1 = Format(DateValue(newDate), "d-mmmm-yy h:mm")
    End Select
    Attempt 1 just adds the date in dd/mm/yy format, and Attempt 2 adds the date in dd/mm/yy h:mm format, and the time is in a 12 hour clock.

    Any ideas how to make Attempt 2 use the correct format (i.e. d-mmmm-yy h:mm)
    We're a Kingdom, and we're United!!

  4. #4
    Managed to fix it I think....thanks for your help!
    We're a Kingdom, and we're United!!

  5. #5
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    Don't forget to mark this thread as "Solved" using the Thread Tools at the top of the thread.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •