Two Digit Year

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

    Two Digit Year

    I feel embarassed asking such a simple question ---

    Year(Date()) returns 2004; I want only the last two digits.
    Format(Year(Dat e()),"yy") returns 05. How do I get 04?

    Thanks!

    Mark


  • Jerry Boone

    #2
    Re: Two Digit Year

    myYear = Right(Year(Date ()), 2)


    --
    Jerry Boone
    Analytical Technologies, Inc.

    Secure Hosting and Development Solutions for ASP, ASP.NET, SQL Server, and
    Access


    "Mark" <mmorrow@earthl ink.net> wrote in message
    news:f1kKb.2723 5$IM3.26368@new sread3.news.atl .earthlink.net. ..[color=blue]
    > I feel embarassed asking such a simple question ---
    >
    > Year(Date()) returns 2004; I want only the last two digits.
    > Format(Year(Dat e()),"yy") returns 05. How do I get 04?
    >
    > Thanks!
    >
    > Mark
    >
    >[/color]


    Comment

    • Wayne Morgan

      #3
      Re: Two Digit Year

      I just checked it and mine does the same thing. It appears to be a bug. I'll
      pass it on. As a work-around, Jerry's suggestion should work.

      --
      Wayne Morgan
      Microsoft Access MVP


      "Mark" <mmorrow@earthl ink.net> wrote in message
      news:f1kKb.2723 5$IM3.26368@new sread3.news.atl .earthlink.net. ..[color=blue]
      > I feel embarassed asking such a simple question ---
      >
      > Year(Date()) returns 2004; I want only the last two digits.
      > Format(Year(Dat e()),"yy") returns 05. How do I get 04?[/color]


      Comment

      • Wayne Morgan

        #4
        Re: Two Digit Year

        Disregard my previous message, I couldn't see the forest for the trees.

        Format(Date(), "yy") should give you the correct answer. The problem is the
        Year(Date()) isn't a date, it is an integer. Access/VBA handles dates as
        integers. 2004 would give you a date 2004 days after VBA's root date which
        happens to be a day in 1905.

        --
        Wayne Morgan
        Microsoft Access MVP


        "Mark" <mmorrow@earthl ink.net> wrote in message
        news:f1kKb.2723 5$IM3.26368@new sread3.news.atl .earthlink.net. ..[color=blue]
        > I feel embarassed asking such a simple question ---
        >
        > Year(Date()) returns 2004; I want only the last two digits.
        > Format(Year(Dat e()),"yy") returns 05. How do I get 04?
        >
        > Thanks!
        >
        > Mark
        >
        >[/color]


        Comment

        • Jerry Boone

          #5
          Re: Two Digit Year

          What about Format(now(), "yy")?

          Strange though - they both work yield 04 for me in A-2K and A-XP in the
          immediate window doing...

          ?Format(now(), "yy")
          or
          ?Format(date(), "yy")


          --
          Jerry Boone
          Analytical Technologies, Inc.

          Secure Hosting and Development Solutions for ASP, ASP.NET, SQL Server, and
          Access


          "Wayne Morgan" <comprev_gothro ughthenewsgroup @hotmail.com> wrote in message
          news:KOkKb.5327 3$jo7.51777@new ssvr31.news.pro digy.com...[color=blue]
          > Disregard my previous message, I couldn't see the forest for the trees.
          >
          > Format(Date(), "yy") should give you the correct answer. The problem is[/color]
          the[color=blue]
          > Year(Date()) isn't a date, it is an integer. Access/VBA handles dates as
          > integers. 2004 would give you a date 2004 days after VBA's root date which
          > happens to be a day in 1905.
          >
          > --
          > Wayne Morgan
          > Microsoft Access MVP
          >
          >
          > "Mark" <mmorrow@earthl ink.net> wrote in message
          > news:f1kKb.2723 5$IM3.26368@new sread3.news.atl .earthlink.net. ..[color=green]
          > > I feel embarassed asking such a simple question ---
          > >
          > > Year(Date()) returns 2004; I want only the last two digits.
          > > Format(Year(Dat e()),"yy") returns 05. How do I get 04?
          > >
          > > Thanks!
          > >
          > > Mark
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Lyle Fairfield

            #6
            Re: Two Digit Year

            "Wayne Morgan" <comprev_gothro ughthenewsgroup @hotmail.com> wrote in
            news:KOkKb.5327 3$jo7.51777@new ssvr31.news.pro digy.com:
            [color=blue]
            > Access/VBA handles dates as integers.[/color]

            I think VBA handles dates as dates.
            no ... not as doubles.

            --
            Lyle
            (for e-mail refer to http://ffdba.com/contacts.htm)

            Comment

            • Jerry Boone

              #7
              Re: Two Digit Year

              That's what I thought. When I convert integers, I add them to whatever the
              begin date for the language starts at... for instance, this is for finding
              the date for Professional Basic (1/1/1980)...

              myDate = DateAdd("d", iDays, "1/1/1980")

              I would think that it returning 05 would have more than likely been for
              todays day - 1..."05"....200 4, but don't know how that worked out, unless
              Mark mistyped the problem.

              --
              Jerry Boone
              Analytical Technologies, Inc.

              Secure Hosting and Development Solutions for ASP, ASP.NET, SQL Server, and
              Access


              "Lyle Fairfield" <MissingAddress @Invalid.Com> wrote in message
              news:Xns9467AB6 89364DFFDBA@130 .133.1.4...[color=blue]
              > "Wayne Morgan" <comprev_gothro ughthenewsgroup @hotmail.com> wrote in
              > news:KOkKb.5327 3$jo7.51777@new ssvr31.news.pro digy.com:
              >[color=green]
              > > Access/VBA handles dates as integers.[/color]
              >
              > I think VBA handles dates as dates.
              > no ... not as doubles.
              >
              > --
              > Lyle
              > (for e-mail refer to http://ffdba.com/contacts.htm)[/color]


              Comment

              • Wayne Morgan

                #8
                Re: Two Digit Year

                In the immediate window try

                ?Format(0,"Long Date")
                Saturday, 30 December 1899
                (this is the base date)

                The date portion is the integer part and the time (fraction of a day) is the
                decimal portion. So

                ?Format(2004, "Long Date")
                Monday, 26 June 1905

                or 2004 days after 30 Dec 1899.

                That is why you can simply add a number to a date to get another date.

                ?#3/1/2004#+3
                3/4/2004

                --
                Wayne Morgan
                Microsoft Access MVP


                "Lyle Fairfield" <MissingAddress @Invalid.Com> wrote in message
                news:Xns9467AB6 89364DFFDBA@130 .133.1.4...[color=blue]
                > "Wayne Morgan" <comprev_gothro ughthenewsgroup @hotmail.com> wrote in
                > news:KOkKb.5327 3$jo7.51777@new ssvr31.news.pro digy.com:
                >[color=green]
                > > Access/VBA handles dates as integers.[/color]
                >
                > I think VBA handles dates as dates.
                > no ... not as doubles.
                >
                > --
                > Lyle
                > (for e-mail refer to http://ffdba.com/contacts.htm)[/color]


                Comment

                • Lyle Fairfield

                  #9
                  Re: Two Digit Year

                  "Wayne Morgan" <comprev_gothro ughthenewsgroup @hotmail.com> wrote in
                  news:NDlKb.2164 4$q91.6670@news svr33.news.prod igy.com:
                  [color=blue]
                  > That is why you can simply add a number to a date to get another date.[/color]

                  But is it the right date?

                  --------------------------------------------------

                  ?format(#12/30/1899#+(-0.5),"\#mm\/dd\/yyyy hh:nn")
                  #12/30/1899 12:00

                  ---------------------------------------------------

                  ?format(#12/30/1899#+(0.5),"\# mm\/dd\/yyyy hh:nn")
                  #12/30/1899 12:00

                  ---------------------------------------------------

                  Can adding (-0.5) give the same result as adding (0.5)?

                  Dates is dates.

                  And if we want to subtract 1/2 a day from a date it may be better to use
                  DateAdd and the long count of a date time interval.

                  ---------------------------------------------------

                  ?format(DateAdd ("h",(-12), #12/30/1899#),"\#mm\/dd\/yyyy hh:nn")
                  #12/29/1899 12:00

                  ---------------------------------------------------

                  from the help file:
                  "If number [in DateAdd(interva l, number, date)] isn't a Long value, it is
                  rounded to the nearest whole number before being evaluated."
                  My experience (not very much) is that number is truncated, not rounded.

                  --
                  Lyle
                  (for e-mail refer to http://ffdba.com/contacts.htm)

                  Comment

                  • Wayne Morgan

                    #10
                    Re: Two Digit Year

                    The problem appears to be that you've hit the limit because this is the base
                    date. It doesn't want to go back beyond that to the 29th.

                    ?format(#12/31/1899#+(0.5),"mm \/dd\/yyyy ttttt")
                    12/31/1899 12:00:00 PM

                    ?format(#12/31/1899#+(-0.5),"mm\/dd\/yyyy ttttt")
                    12/30/1899 12:00:00 PM

                    As you can see, if I move up to the 31st and try the same thing, it works.

                    --
                    Wayne Morgan
                    Microsoft Access MVP


                    "Lyle Fairfield" <MissingAddress @Invalid.Com> wrote in message
                    news:Xns9467DF6 E2C318FFDBA@130 .133.1.4...[color=blue]
                    > "Wayne Morgan" <comprev_gothro ughthenewsgroup @hotmail.com> wrote in
                    > news:NDlKb.2164 4$q91.6670@news svr33.news.prod igy.com:
                    >[color=green]
                    > > That is why you can simply add a number to a date to get another date.[/color]
                    >
                    > But is it the right date?
                    >
                    > --------------------------------------------------
                    >
                    > ?format(#12/30/1899#+(-0.5),"\#mm\/dd\/yyyy hh:nn")
                    > #12/30/1899 12:00
                    >
                    > ---------------------------------------------------
                    >
                    > ?format(#12/30/1899#+(0.5),"\# mm\/dd\/yyyy hh:nn")
                    > #12/30/1899 12:00
                    >
                    > ---------------------------------------------------
                    >
                    > Can adding (-0.5) give the same result as adding (0.5)?
                    >
                    > Dates is dates.
                    >
                    > And if we want to subtract 1/2 a day from a date it may be better to use
                    > DateAdd and the long count of a date time interval.
                    >
                    > ---------------------------------------------------
                    >
                    > ?format(DateAdd ("h",(-12), #12/30/1899#),"\#mm\/dd\/yyyy hh:nn")
                    > #12/29/1899 12:00
                    >
                    > ---------------------------------------------------
                    >
                    > from the help file:
                    > "If number [in DateAdd(interva l, number, date)] isn't a Long value, it is
                    > rounded to the nearest whole number before being evaluated."
                    > My experience (not very much) is that number is truncated, not rounded.
                    >
                    > --
                    > Lyle
                    > (for e-mail refer to http://ffdba.com/contacts.htm)[/color]


                    Comment

                    • Lyle Fairfield

                      #11
                      Re: Two Digit Year

                      "Wayne Morgan" <comprev_gothro ughthenewsgroup @hotmail.com> wrote in
                      news:_JpKb.5385 3$hK.17968@news svr31.news.prod igy.com:
                      [color=blue]
                      > The problem appears to be that you've hit the limit because this is the
                      > base date. It doesn't want to go back beyond that to the 29th.
                      >
                      > ?format(#12/31/1899#+(0.5),"mm \/dd\/yyyy ttttt")
                      > 12/31/1899 12:00:00 PM
                      >
                      > ?format(#12/31/1899#+(-0.5),"mm\/dd\/yyyy ttttt")
                      > 12/30/1899 12:00:00 PM
                      >
                      > As you can see, if I move up to the 31st and try the same thing, it
                      > works.[/color]

                      sigh ...

                      --
                      Lyle
                      (for e-mail refer to http://ffdba.com/contacts.htm)

                      Comment

                      • Wayne Morgan

                        #12
                        Re: Two Digit Year



                        --
                        Wayne Morgan
                        MS Access MVP


                        "Lyle Fairfield" <MissingAddress @Invalid.Com> wrote in message
                        news:Xns9467E43 2069FCFFDBA@130 .133.1.4...[color=blue]
                        >
                        > sigh ...
                        >
                        > --
                        > Lyle
                        > (for e-mail refer to http://ffdba.com/contacts.htm)[/color]


                        Comment

                        • Wayne Morgan

                          #13
                          Re: Two Digit Year

                          Lyle,

                          I had to ask about this one, but got an answer.

                          Since the integer portion is the day and the decimal is the time and 30
                          December 1899 is day zero what you get by subtracting .5 from 0 is -0.5.
                          This is still day 0 (the integer portion) so you still get 30 Dec 1899. You
                          have to go to -1 to get 29 Dec 1899. In other words, there is no difference
                          between -0 and +0, they are the same number. The date handling breaks the
                          number apart into its 2 components, the integer and the decimal, and then
                          gives the result. The mathematics though, is performed as expected. So, the
                          number is being treated differently by the date conversion than it is by the
                          math routines.

                          So you have found a place that simply adding and subtracting instead of
                          using DateAdd or other functions may cause problems. However, in the dates
                          most people deal in, this probably won't show up.

                          --
                          Wayne Morgan
                          Microsoft Access MVP


                          "Lyle Fairfield" <MissingAddress @Invalid.Com> wrote in message
                          news:Xns9467DF6 E2C318FFDBA@130 .133.1.4...[color=blue]
                          > "Wayne Morgan" <comprev_gothro ughthenewsgroup @hotmail.com> wrote in
                          > news:NDlKb.2164 4$q91.6670@news svr33.news.prod igy.com:
                          >[color=green]
                          > > That is why you can simply add a number to a date to get another date.[/color]
                          >
                          > But is it the right date?
                          >
                          > --------------------------------------------------
                          >
                          > ?format(#12/30/1899#+(-0.5),"\#mm\/dd\/yyyy hh:nn")
                          > #12/30/1899 12:00
                          >
                          > ---------------------------------------------------
                          >
                          > ?format(#12/30/1899#+(0.5),"\# mm\/dd\/yyyy hh:nn")
                          > #12/30/1899 12:00
                          >
                          > ---------------------------------------------------
                          >
                          > Can adding (-0.5) give the same result as adding (0.5)?
                          >
                          > Dates is dates.
                          >
                          > And if we want to subtract 1/2 a day from a date it may be better to use
                          > DateAdd and the long count of a date time interval.
                          >
                          > ---------------------------------------------------
                          >
                          > ?format(DateAdd ("h",(-12), #12/30/1899#),"\#mm\/dd\/yyyy hh:nn")
                          > #12/29/1899 12:00
                          >
                          > ---------------------------------------------------
                          >
                          > from the help file:
                          > "If number [in DateAdd(interva l, number, date)] isn't a Long value, it is
                          > rounded to the nearest whole number before being evaluated."
                          > My experience (not very much) is that number is truncated, not rounded.
                          >
                          > --
                          > Lyle
                          > (for e-mail refer to http://ffdba.com/contacts.htm)[/color]


                          Comment

                          Working...