473,386 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 24582
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

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

Similar topics

3
by: Killer | last post by:
How can i calculate the last day of the previous month? Help me,please
5
by: Ray via SQLMonster.com | last post by:
Hi there, See if you can help me with the following: I need to write an SQL code that will return me: The 1st day & the Last day of the Previous Month in the following format...
13
by: SimonC | last post by:
I would like to return data from the last 2 weeks of each given month in Javascript, but in 2 formats. So, the penultimate week (Monday to Sunday) and the last week (Monday to ??) I'm not...
7
by: MLH | last post by:
Public Function GetLastDayOfMonth(ByVal dtDay As Date) As Date '************************************************************************** ' Accepts a date. Determines month & year of the date....
9
by: brymcguire | last post by:
Hi, I have a requirement to design a query that identifies items sold between two dates. There is a 'SoldDate' datetime field used to register what date the item was sold. The query needs...
10
by: ashore | last post by:
Guys, the line below just returned "Dec 07" as the date for one month back from today. Hardly life-threatening, but any thoughts? <?php print date("M `y", mktime(0, 0, 0, date("m")-1, date("d"),...
3
by: drkknightbatman | last post by:
Hi, I am pretty new to Access 2003 and SQL. I am trying to have a count show up in a query to show specific barcode prefix data between the first and the last day of the previous month. I want to do...
1
by: guyborn | last post by:
I have been trying to get data from the database from the of the previous month to the second last week of the current month.I only managed to get data from the previous month to today's date. ...
0
by: marlberg | last post by:
Platform: Windows2000, WindowsXP, Windows Vista, etc Language: C#, ASP.NET Pre-compiled Libraries: Enterprise Library 3.0 full I have a requirement to implement in and display in C# and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.