|
-
Oct 3rd, 2000, 04:17 AM
#1
Thread Starter
Addicted Member
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
-
Oct 3rd, 2000, 04:25 AM
#2
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]
-
Oct 3rd, 2000, 04:39 AM
#3
Addicted Member
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é
-
Oct 3rd, 2000, 04:50 AM
#4
Thread Starter
Addicted Member
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
-
Oct 3rd, 2000, 04:54 AM
#5
Addicted Member
You're welcome, and you are right sometimes you just can't see!!!
André
-
Oct 3rd, 2000, 06:40 AM
#6
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?
-
Oct 3rd, 2000, 07:45 AM
#7
Thread Starter
Addicted Member
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
-
Oct 3rd, 2000, 08:07 AM
#8
_______
<?>
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
-
Oct 3rd, 2000, 09:25 AM
#9
Instead of the case statement can you not say:
MyMonth = format(MyMonth & "/" & MyDay & "/" & MyYear,"mmm") & myDay & ", " & myYear
-
Oct 3rd, 2000, 09:45 AM
#10
Frenzied Member
come on HeSaidJoe, your answer is just nonsense! 
hermanvd's works
sarun's is probably dependant on regional settings
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|