Results 1 to 2 of 2

Thread: Printing out Months and Years between two dates

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2001
    Posts
    152

    Question Printing out Months and Years between two dates

    I have two dates, example would be 10/1/2006 and the current date 9/8/2007. How would I loop through and print out every month between these two dates with the year?

    October 2006
    November 2006
    December 2006
    January 2007
    etc...
    September 2007

    Any ideas? and thanks!

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Printing out Months and Years between two dates

    try like this
    vb Code:
    1. For i = DateDiff("m", "10/1/2006", "9/8/2007") To 0 Step -1
    2.     Print Format(DateAdd("m", -i, "9/8/2007"), "mmmm yyyy")
    3. Next
    on my system it prints from january 2006 to august 2007, but that is because my locale date setting is dd/mm/yyyy, should work right on systems where locale date is mm/dd/yyyy

    as there is no way to tell from 10/1/2006 which is the month (unless you know) there is no foolproof way to code it so that it will always be correct, if you use a date format like oct 1 2006 then it can always be correct, using now or date for the current month will always be correct
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width