If a given date is less than the end of the next month (1 Viewer)

KitaYama

Well-known member
Local time
Today, 22:57
Joined
Jan 6, 2022
Messages
1,553
I can write a function to calculate this, but I thought there may be a simpler way to validate this date (maybe using DateDiff?)

I'm trying to validate if a date typed in a textbox is less than the end of the next month.

Any kind of suggestions is much appreciated.
 

plog

Banishment Pending
Local time
Today, 08:57
Joined
May 11, 2011
Messages
11,657
Function would be best way. I assume you actually meant less than or equal to the last day of next month not just less than that date. Which is logically equivalent to less than the first date of 2 months away. That logic is easier to implement.

I wouldn't do it with datediff but just a straight comparison between date entered and then determine the date of the first day of the month for 2 months away. It's easier to figure out the first date of a month (it's day value is always 1) than the last date of the month (28,29,30,31).

You would use DateValue, Month, Year and DateAdd to make that date.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 21:57
Joined
May 7, 2009
Messages
19,247
try:

If Day(Me.DateControl) < Day(DateSerial(Year(Me.DateControl), Month(Me.DateControl) + 2, 0)) Then
' the day is less
End If
 

KitaYama

Well-known member
Local time
Today, 22:57
Joined
Jan 6, 2022
Messages
1,553
try:

If Day(Me.DateControl) < Day(DateSerial(Year(Me.DateControl), Month(Me.DateControl) + 2, 0)) Then
' the day is less
End If
I never thought of DateSerial. Now that you've used it, is there any reason you didn't used it like:

if MyDate< DateSerial(Year(Date), Month(Date) + 2, 1)

Thanks.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 21:57
Joined
May 7, 2009
Messages
19,247
I'm trying to validate if a date typed in a textbox is less than the end of the next month.
maybe i did not understand it fully.
"the end of next month" do you mean the last Day of that month?
if so, then i use the Day() function.

if you think what you need is different then do so.
 

Solo712

Registered User.
Local time
Today, 09:57
Joined
Oct 19, 2012
Messages
828
try:

If Day(Me.DateControl) < Day(DateSerial(Year(Me.DateControl), Month(Me.DateControl) + 2, 0)) Then
' the day is less
End If
maybe i did not understand it fully.
"the end of next month" do you mean the last Day of that month?
if so, then i use the Day() function.

if you think what you need is different then do so.
No Arnel, the OP was quite clear. You need to compare the date text box with reference to the current month, and determine whether it falls to next month. DateDiff or the DateSerial as KityYama constructed it are the ways to go.

Best,
Jiri
 

Users who are viewing this thread

Top Bottom