Date function spanning across time periods.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Robin Merrill
    New Member
    • Dec 2011
    • 2

    Date function spanning across time periods.

    Hi.

    I need the syntax in Access 2007 that will subtract one month from the current date and always get the correct prior month, i.e. in January 2012, month(date())-1 would pull December 2011 dates and in July 2012 it would pull June 2012.

    Thanks for your help.
    Robin
  • Scott W
    New Member
    • Dec 2011
    • 3

    #2
    This will give the date one month ago:
    Code:
    DateAdd("m",-1,Date())
    "m" tells it to count by months, -1 is the interval, and the Date() is just the date function, but it can be any Date variable.
    Last edited by NeoPa; Dec 6 '11, 01:38 AM. Reason: Removed unnecessary quote and added mandatory [CODE] tags

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      Use the DateAdd function to add a negative month.
      Code:
      DateAdd("m", -1, someDateField)

      Comment

      • sierra7
        Recognized Expert Contributor
        • Sep 2007
        • 446

        #4
        Code:
        DateAdd("m", -1, Date)
        S7

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32662

          #5
          Three good answers. I can also confirm that this handles difficult months correctly too.

          For instance, using a value for 31st March returns the last day of February (as illustrated below) :

          Code:
          ?DateAdd("m",-1,#31/3/2011#)
          28/02/2011
          If I may be allowed to make a suggestion, Post #2, being the first posted and answering pretty fully as well, might be a good selection for Best Answer (With no disrespect to either of the other posts which were equally correct).

          Comment

          • Robin Merrill
            New Member
            • Dec 2011
            • 2

            #6
            Thanks for all of your quick responses. I belive I need to use this funcgtion instead of the simple month(date()) -1 in my reports that will run every month for multiple years, is that right?

            Thanks again.
            Robin

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32662

              #7
              Code:
              Month(Date()) - 1
              This won't work very well for January.

              Only you know exactly what you need. Certainly what you say sounds sensible but we're not in a good position to cofirm as we don't know what your requirements are.

              Comment

              Working...