Adding to a date

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Roy Riddex

    Adding to a date

    How do I add to a date? I thought the following code would work but it
    doesn't like it. I'm trying to add 6 months (182 days) onto a date.

    Option Explicit

    Private Sub Form_Load()
    Dim Answer As Integer
    Dim GivenDate As Date
    GivenDate = "2/1/2004"
    Answer = GivenDate + 182
    Label1.Caption = Answer
    End Sub


  • Rick Rothstein

    #2
    Re: Adding to a date

    > How do I add to a date? I thought the following code would work but it[color=blue]
    > doesn't like it. I'm trying to add 6 months (182 days) onto a date.
    >
    > Option Explicit
    >
    > Private Sub Form_Load()
    > Dim Answer As Integer
    > Dim GivenDate As Date
    > GivenDate = "2/1/2004"
    > Answer = GivenDate + 182
    > Label1.Caption = Answer
    > End Sub[/color]

    Look up the DateAdd function in your help files. Also, while assigning
    literal date values with a String, it is better to do it using # signs
    instead.

    GivenDate = #2/1/2004#

    Rick - MVP


    Comment

    • Roy Riddex

      #3
      Re: Adding to a date


      "Rick Rothstein" <rickNOSPAMnews @NOSPAMcomcast. net> wrote in message
      news:[email protected]. ..[color=blue][color=green]
      > > How do I add to a date? I thought the following code would work but it
      > > doesn't like it. I'm trying to add 6 months (182 days) onto a date.
      > >
      > > Option Explicit
      > >
      > > Private Sub Form_Load()
      > > Dim Answer As Integer
      > > Dim GivenDate As Date
      > > GivenDate = "2/1/2004"
      > > Answer = GivenDate + 182
      > > Label1.Caption = Answer
      > > End Sub[/color]
      >
      > Look up the DateAdd function in your help files. Also, while assigning
      > literal date values with a String, it is better to do it using # signs
      > instead.
      >
      > GivenDate = #2/1/2004#
      >
      > Rick - MVP
      >
      >[/color]

      Thanks Rick. Only problem is the college dont have a license to give out the
      MSDN library, so the help files on my copy of VB6 dont work. Can someone
      please help? I want to be able to add 182 days OR 6 months. Thanks


      Comment

      • J French

        #4
        Re: Adding to a date

        On Sat, 6 Mar 2004 10:01:29 -0000, "Roy Riddex"
        <roy_riddexNOSP [email protected] o.uk> wrote:

        <snip>
        [color=blue]
        >
        >Thanks Rick. Only problem is the college dont have a license to give out the
        >MSDN library, so the help files on my copy of VB6 dont work.[/color]

        That is disgraceful
        [color=blue]
        >Can someone
        >please help? I want to be able to add 182 days OR 6 months. Thanks[/color]

        NewDate = DateAdd(interva l, number, OldDate)

        The 'Interval' strings are :-

        yyyy Year
        q Quarter
        m Month
        y Day of year
        d Day
        w Weekday
        ww Week
        h Hour
        n Minute
        s Second

        eg: 6 months
        NewDate = DateAdd( "m", 6, OldDate)

        eg: 182 days
        NewDate = DateAdd( "d", 182, OldDate)


        Comment

        • Rick Rothstein

          #5
          Re: Adding to a date

          > <snip>[color=blue]
          >[color=green]
          > >
          > >Thanks Rick. Only problem is the college dont have a license to give out[/color][/color]
          the[color=blue][color=green]
          > >MSDN library, so the help files on my copy of VB6 dont work.[/color]
          >
          > That is disgraceful
          >[color=green]
          > >Can someone
          > >please help? I want to be able to add 182 days OR 6 months. Thanks[/color]
          >
          > NewDate = DateAdd(interva l, number, OldDate)
          >
          > The 'Interval' strings are :-
          >
          > yyyy Year
          > q Quarter
          > m Month
          > y Day of year
          > d Day
          > w Weekday
          > ww Week
          > h Hour
          > n Minute
          > s Second
          >
          > eg: 6 months
          > NewDate = DateAdd( "m", 6, OldDate)
          >
          > eg: 182 days
          > NewDate = DateAdd( "d", 182, OldDate)[/color]

          First, I'd like to not that adding 182 days (as you originally proposed)
          will not always cover an exact 6-month period of time... the starting date
          and the varying number of days for the subsequent months will affect it. Use
          the "m" interval argument coupled with the "6" for the exact answer no
          matter what your OldDate is.

          Now, yes, I agree with JFrench's statement... it is disgraceful that your
          college didn't acquire the Help files for its students... how short-sighted
          can you get? If you go to this link...



          it will take you to the online Microsoft Help files. They are not as
          convenient to use as a local copy of the MSDN would be, but they'll do.
          Follow this "tree" down to the VB6 section covering the Language and Control
          references...

          -Visual Tools and Languages
          -Visual Studio 6.0
          -Visual Basic 6.0
          -Product Documentation
          -Reference
          -Language Reference
          -Controls Reference

          under which you will find the syntax for the things you will encounter in
          VB6.

          Rick - MVP


          Comment

          • Steve Gerrard

            #6
            Re: Adding to a date


            "J French" <erewhon@nowher e.com> wrote in message
            news:4049a442.2 [email protected] tclick.com...[color=blue]
            > On Sat, 6 Mar 2004 10:01:29 -0000, "Roy Riddex"
            > <roy_riddexNOSP [email protected] o.uk> wrote:
            >
            > <snip>
            >[color=green]
            > >
            > >Thanks Rick. Only problem is the college dont have a license to give[/color][/color]
            out the[color=blue][color=green]
            > >MSDN library, so the help files on my copy of VB6 dont work.[/color]
            >
            > That is disgraceful
            >[/color]

            If I am not mistaken, at work when we got MSDN library for a while
            (circa 2000), they changed the license to say more or less "one copy is
            all you need for all the people where you work to use this". So I think
            your college can legally allow multiple users to install their one copy
            (hopefully they have one copy).


            Comment

            • Roy Riddex

              #7
              Re: Adding to a date

              Thanks for all your help. Unfortunately the MSDN library problem has been
              ongoing since I started at college last August. Not even the in-house
              college machines have it installed so there is no chance of me aquiring a
              copy for home. However, as the course gets steadily more difficult I think I
              may need to 'aquire' a copy from somewhere else.

              Roy


              Comment

              • thfc1961

                #8
                Re: Adding to a date

                Label1.Caption = Format(Answer," mm/dd/yyyy")

                "Roy Riddex" <roy_riddexNOSP [email protected] o.uk> wrote in message
                news:on92c.4830 [email protected] er.co.uk...[color=blue]
                > How do I add to a date? I thought the following code would work but it
                > doesn't like it. I'm trying to add 6 months (182 days) onto a date.
                >
                > Option Explicit
                >
                > Private Sub Form_Load()
                > Dim Answer As Integer
                > Dim GivenDate As Date
                > GivenDate = "2/1/2004"
                > Answer = GivenDate + 182
                > Label1.Caption = Answer
                > End Sub
                >
                >[/color]


                Comment

                • Roy Riddex

                  #9
                  Re: Adding to a date

                  [color=blue]
                  > Label1.Caption = Format(Answer," mm/dd/yyyy")[/color]

                  Erm....did you read the OP? I had asked how to add days or months onto a
                  date. I didn't ask how to format a date, but thanks anyway.


                  Comment

                  • QuickHare

                    #10
                    Re: Adding to a date

                    > > Label1.Caption = Format(Answer," mm/dd/yyyy")[color=blue]
                    >
                    > Erm....did you read the OP? I had asked how to add days or months onto a
                    > date. I didn't ask how to format a date, but thanks anyway.[/color]

                    The OP asked about adding to the date. He did this by using Answer = GivenDate +
                    182, which is how to add to the date. To reformat it into a human-readable date,
                    you use Format. The only point I'd raise is to use Format$() as this converts it
                    into a string automatically, and saves the computer time (especially if done in
                    a loop).

                    --
                    QuickHare
                    (QuickHare "at" Hotmail "dot" com)


                    Comment

                    Working...