Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Calculation 3

Status
Not open for further replies.

ousoonerjoe

Programmer
Jun 12, 2007
925
US
Is there a simple way of calculating the last day of the month and displaying in MM/DD/YYYY format? There's a trick, it must also be able to account for year change as well.

for example: 1/10/2008 minus 1 month = 12/10/2008

What i need is the last day of the previous month.

Any thoughts are appreciated.

"If I were to wake up with my head sewn to the carpet, I wouldn't be more surprised than I am right now.
 





Code:
LastDayOfPrevMO = DateSerial(Year(TheDate),Month(TheDate),0)


Skip,

[glasses] When a diminutive clarvoyant had disappeared from detention, headlines read...
Small Medium at Large[tongue]
 




for the current date...
Code:
LastDayOfPrevMO = DateSerial(Year(Date),Month(Date),0)


Skip,

[glasses] When a diminutive clarvoyant had disappeared from detention, headlines read...
Small Medium at Large[tongue]
 



Just keep in mind that Format returns a STRING and not a DATE.

Skip,

[glasses] When a diminutive clarvoyant had disappeared from detention, headlines read...
Small Medium at Large[tongue]
 
Thanks a bunch, Skip. I was able to come up with:

Dim RptDate As Date
RptDate = (Month(Now) & "/1/" & Year(Now))
RptDate = RptDate - 1

but I'll use your's since it's much cleaner.

Thanks again. Have a star.

"If I were to wake up with my head sewn to the carpet, I wouldn't be more surprised than I am right now.
 
Another thing to keep in mind about yours, ou, is that it depends on the mmddyyyy format. It wouldn't run properly in Britain, for example, because they use the ddmmyyyy format. So Skip's is not only cleaner, it works better too. :)
 
And just to show that there's more than one way to skin a cat:

LastDayOfPrevMO = TheDate - Day(TheDate) - 1
 




I think that you wanted that to be...
Code:
LastDayOfPrevMO = Date - Day(Date)
...and it all makes perfect sense, when you understand that a Date is a NUMBER and Date - 1 is yesterday.

Skip,

[glasses] When a diminutive clarvoyant had disappeared from detention, headlines read...
Small Medium at Large[tongue]
 
>I think that you wanted

Indeed. No idea how the - 1 got in there ...
 




What does Clair have to do with it???

Skip,

[glasses] When a diminutive clarvoyant had disappeared from detention, headlines read...
Small Medium at Large[tongue]
 
Next you'll be telling us that

Levis est fortuna.

doesn't mean "blue jeans are expensive!"

(This has gone way OT.)
 
Use 0 for day and Month(Date) + 1 for month
Code:
myMonthend = DateSerial(Year(Date),Month(Date)+1,0)

___________________________________________________________
If you want the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
Steam Engine Prints
 
Then we'd look the solution provided by SkipVought, and modify it for the month we require

If

DateSerial(Year(Date),Month(Date),0)

gives us the last day of previous month, then

DateSerial(Year(Date),Month(Date) + 1,0)

gives us the last day of this month


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top