I want to compare two dates with today date. Does isAfter and isBefore best for this? isAfter and isBefore cant detect one day changes. Lets say:
If today is 20 Nov. I put in range 20 Nov-21 Nov.
if(todayDate.isAfter(startDate) && todayDate.isBefore(endDate))
{
// task
}
This code wont detect that today is in range. OR / || is not applicable because I have a set of range to be tested. Any idea on this?
This will solve it: Just add a check for whether today's date it either the start or end date which you will have to implement yourself
if( (todayDate.isAfter(startDate) && todayDate.isBefore(endDate) ) || (todayDate.isEqual(startDate) || todayDate.isEqual(endDate) )
{
// task
}
This is because isAfter
and isBefore
are both strict.
Edit: A better more logical solution:
if(!todayDate.isAfter(endDate) && !todayDate.isBefore(startDate))
{
// task
}
By negating isAfter
, it becomes endDate or before.
By negating isBefore
, it becomes startDate or after.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With