Previous Month Date (1 Viewer)

MattioMatt

Registered User.
Local time
Today, 16:46
Joined
Apr 25, 2017
Messages
99
Hello,

I'm trying to get the last date of the previous month based on a current month.

So for example if the current end month is 30/06/2018, I'd like it to return 31/05/2018.

I have a text box called txtCurrentDate (Value: 30/06/2018) I then have a second text box called txtPreviousDate with the following
=DateAdd("m",-1,[txtCurrentDate])

However this is pulling back 30/05/2018 but as I want it to be the last day of the previous month I'd like it to pull 31/05/2018. Similiary this issue happens for different dates.

Any ideas much appreciated :)
 

Gasman

Enthusiastic Amateur
Local time
Today, 16:46
Joined
Sep 21, 2011
Messages
14,235
The key is get the first of the current month and subtract 1 day.
 

Minty

AWF VIP
Local time
Today, 16:46
Joined
Jul 26, 2013
Messages
10,368
As Gasman has said
Code:
DateSerial(Year(Date()), Month(Date()), 0)

Will do what you want. To experiment open the VBA editor and presgg Ctrl +G to bring up the immediate window type
? DateSerial(Year(Date()), Month(Date()), 0)
and hit enter
Code:
 ? DateSerial(Year(Date()), Month(Date()), 0) 
30/06/2018

The ? means print the result. Handy for fiddling with these type of expressions.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 08:46
Joined
Aug 30, 2003
Messages
36,124
A trick is using 0 for the day so you don't have to subtract:

?dateserial(year(date),month(date),0)
6/30/2018
 

Gasman

Enthusiastic Amateur
Local time
Today, 16:46
Joined
Sep 21, 2011
Messages
14,235
A trick is using 0 for the day so you don't have to subtract:

?dateserial(year(date),month(date),0)
6/30/2018


I should have remembered that, as I have only recently posted it in another thread.:banghead:
 

MattioMatt

Registered User.
Local time
Today, 16:46
Joined
Apr 25, 2017
Messages
99
Thanks all this is perfect and exactly what I'm looking for :)
 

CJ_London

Super Moderator
Staff member
Local time
Today, 16:46
Joined
Feb 19, 2013
Messages
16,607
the other way is

=DateAdd("d",-Day([txtCurrentDate]),[txtCurrentDate])
 

Users who are viewing this thread

Top Bottom