469,275 Members | 1,772 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

Get last and first day of previous month

274 100+
hello
I am using following code to access first and last date of current month

StartingDate_msk.Text = Format$(Date, "dd/mm/yyyy")
EndingDate_msk.Text = Format$(MonthLastDay(Date), "dd/mm/yyyy")
can some one help me how I can find fist and last date of previous month?

chill
farhana
Sep 26 '07 #1
6 23360
hariharanmca
1,977 1GB
hello
I am using following code to access first and last date of current month

StartingDate_msk.Text = Format$(Date, "dd/mm/yyyy")
EndingDate_msk.Text = Format$(MonthLastDay(Date), "dd/mm/yyyy")
can some one help me how I can find fist and last date of previous month?

chill
farhana
First day will be 1

and to get last day
Expand|Select|Wrap|Line Numbers
  1. Day(DateSerial(Year(Now), Month(Now), 0))
Below will return last date
Expand|Select|Wrap|Line Numbers
  1. DateSerial(Year(Now), Month(Now), 0)
Sep 26 '07 #2
creative1
274 100+
thanks
But last day of previous month worked but first day of previous month didn't.
this is what I get when I use your code.I just formatted it to match my requirments.


StartingDate_msk.Text = Format$(Day(DateSerial(Year(Now), Month(Now), 0)), "dd/mm/yyyy")

30/01/1900 i.e incorrect

EndingDate_msk.Text = Format$(DateSerial(Year(Now), Month(Now), 0), "dd/mm/yyyy")
31/08/2007 i.e correct


any suggestions.
farhana
Sep 26 '07 #3
hariharanmca
1,977 1GB
thanks
But last day .....................
Expand|Select|Wrap|Line Numbers
  1. StartingDate_msk.Text = Format$(Day(DateSerial(Year(Now), Month(Now), 0)), "dd/mm/yyyy")
30/01/1900 i.e incorrect
Expand|Select|Wrap|Line Numbers
  1. EndingDate_msk.Text = Format$(DateSerial(Year(Now), Month(Now), 0), "dd/mm/yyyy")
31/08/2007 i.e correct
For Last day of PrevMonth

Expand|Select|Wrap|Line Numbers
  1. StartingDate_msk.Text = Format$(DateSerial(Year(Now), Month(DateAdd("M", -1, Now)), 0)), "dd/mm/yyyy")
Sep 26 '07 #4
Expand|Select|Wrap|Line Numbers
  1. LastDateOfPreviousMonth = DateAdd("d", -1, CDate((Month(Now)) & "/01/" & Year(Now)))
  2. Dim Yr, Mnth
  3. If Month(Now) = 1 Then
  4.     Yr = Year(Now) - 1
  5.     Mnth = 12
  6. Else
  7.     Yr = Year(Now)
  8.     Mnth = Month(Now) - 1
  9. End If
  10. FirstDateOfPreviousMonth = CDate(Mnth & "/01/" & Year(Now))
Regards
-Shrimant Patel-
&
-Harith Patel-
Sep 26 '07 #5
Killer42
8,435 Expert 8TB
Nice piece of work, shrimant. However, I feel it's better to use DateSerial as it doesn't depend on your PC using a particular date format. For example PCs in the U.S. and Australia would (probably) return different results for CDate function, because the U.S. has the month first, while Oz has the day first.

I'd recommend the following modification of your routine...

Expand|Select|Wrap|Line Numbers
  1. LastDateOfPreviousMonth = DateAdd("d", -1, DateSerial(Year(Now), (Month(Now), 1)
  2. FirstDateOfPreviousMonth = DateSerial(Year(LastDateOfPreviousMonth), Month(LastDateOfPreviousMonth), 1)
By the way, I seem to recall that the DateAdd() function can sometimes return unexpected results when using the "d" (day) interval. If so, try using "y" (day of year) instead.
Sep 26 '07 #6
creative1
274 100+
thanks everyone. It solved my problem. I appreciate your help
Sep 27 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Killer | last post: by
5 posts views Thread by Ray via SQLMonster.com | last post: by
10 posts views Thread by ashore | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.