Results 1 to 10 of 10

Thread: Get last months Date

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    178

    Question

    Hi all,

    Dates always seem to be a problem....I am trying to get last months date, I know it should be easy but its just not working..

    I use the date command to get the current date. I then want to be able to sort of take one month away and get the previous months.....

    Here is the code I am using at the moment.

    Code:
    CurMon = Format(Date, "mmm")
    LasMon = (Month(Date) - 1) & "\" & Day(Date) & "\" & Year(Date)
    LasMon = Format(LasMon, "mmm")
    It doesn't work. If I declare LasMon as a date I receive an error...

    Anyone know the easiest way to do this....

    Cheers for any help

  2. #2
    Guest

    Talking

    hallo

    Have you tried the following:
    dim date as Date
    dim intmonth as integer

    date = DateAdd("m", -1, Now())
    intmonth = Month(date) 'This returns the number 1 to 12

    Hope this helps!

    [Edited by hermanvd on 10-03-2000 at 05:38 AM]

  3. #3
    Addicted Member
    Join Date
    Jun 1999
    Location
    Los Angeles
    Posts
    186
    Hi here you go:

    Code:
    Dim LasMon As String
    Dim dDate As Date
    Dim CurMon As String
    Dim sDay As String
    Dim sYear As String
    
    CurMon = Format(Date, "mmm")
    
    LastMonth = Month(Date) - 1
    sDay = Day(Date)
    sYear = Year(Date)
    
    dDate = sDay & "/" & LastMonth & "/" & sYear
    
    LasMon = Format(dDate, "mmm")
    Notice that I have a format of dd-mm-yyyy.
    Notice also the direction of the slashes, it's slash and not backslash to format a date.

    This code results in sep for my settings.

    cheers

    André

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    178

    Thanks Andre and hermanvd

    To hermanvd,

    I haven't tried your method, but I will sometime today when I get the time.

    Thanks

    Andre,

    Yes, I realised after I posted that the slashes were the wrong way around. I changed them in my code and it worked fine. Sometimes you just don't see it......Maybe I need my eyes testing or something !!!

    Here is what I'm using to fill a combo with the present and last 2 months (Just in case anyone else has the same prob)

    Code:
    With Me.CmbMonths
        For i = 0 To 2
            LasMon = (Month(Date) - i) & "/" & Day(Date) & "/" & Year(Date)
            MyMonth = Format(LasMon, "mmm")
            .AddItem MyMonth
        Next
        .ListIndex = 0
    End With
    Thanks anyway

    Cheers
    Steve

  5. #5
    Addicted Member
    Join Date
    Jun 1999
    Location
    Los Angeles
    Posts
    186
    You're welcome, and you are right sometimes you just can't see!!!

    André

  6. #6
    Guest

    Exclamation

    Hallo

    I'm not sure if there maybe a new problem?! The DateAdd handles the problem you experience with dates in January.

    Date = cdate("2000-01-01")
    Date_Last_Month = DateAdd("m", -1, Date)'Answer = 1999-12-01


    Am I missing something here?

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    178

    Thumbs up Hermanvd

    Sorry Hermanvd,

    I was referring to the other method of calculating the previous months....

    Just had a look at your method and it seems fine

    Thanks a lot for your help
    Steve

  8. #8
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    'this will do the job for you
    '
    Option Explicit
    
    Private Sub Command1_Click()
    'variables for dates
    Dim MyMonth As Variant, myDay As Variant, myYear As Variant
    Date = Format(Date, "mm,dd,yyyy")
    'the assigned date is today's date
    MyMonth = Month(Format(Date, "mm,dd,yyyy"))
    'subtract 1 month
    MyMonth = MyMonth - 1
    'Select the corresponding day for the value of myMonth
    myDay = Day(Date)
    'Select the year
    myYear = Left(Format(Date, "YYYY,MM,DD"), 4)
    
    'once you have it work with it
    Select Case MyMonth
        Case 1
                MyMonth = "Jan " & myDay & ", " & myYear
        Case 2
                MyMonth = "Feb " & myDay & ", " & myYear
            Case 3
                MyMonth = "Mar " & myDay & ", " & myYear
            Case 4
                MyMonth = "Apr " & myDay & ", " & myYear
            Case 5
                MyMonth = "May " & myDay & ", " & myYear
            Case 6
                MyMonth = "Jun " & myDay & ", " & myYear
            Case 7
                MyMonth = "Jul " & myDay & ", " & myYear
            Case 8
                MyMonth = "Aug " & myDay & ", " & myYear
            Case 9
                MyMonth = "Sep " & myDay & ", " & myYear
            Case 10
                MyMonth = "Oct " & myDay & ", " & myYear
            Case 11
                MyMonth = "Nov " & myDay & ", " & myYear
            Case 2
                MyMonth = "Dec " & myDay & ", " & myYear
    End Select
        
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  9. #9
    Guest
    Instead of the case statement can you not say:

    MyMonth = format(MyMonth & "/" & MyDay & "/" & MyYear,"mmm") & myDay & ", " & myYear


  10. #10
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    come on HeSaidJoe, your answer is just nonsense!

    hermanvd's works

    sarun's is probably dependant on regional settings
    Mark
    -------------------

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