How to Check If a Date Is Within a Certain Range

How to check if a date is in a given range?

Converting them to timestamps is the way to go alright, using strtotime, e.g.

$start_date = '2009-06-17';

$end_date = '2009-09-05';

$date_from_user = '2009-08-28';

check_in_range($start_date, $end_date, $date_from_user);


function check_in_range($start_date, $end_date, $date_from_user)
{
// Convert to timestamp
$start_ts = strtotime($start_date);
$end_ts = strtotime($end_date);
$user_ts = strtotime($date_from_user);

// Check that user date is between start & end
return (($user_ts >= $start_ts) && ($user_ts <= $end_ts));
}

How do I check if a date is within a certain range?

boolean isWithinRange(Date testDate) {
return !(testDate.before(startDate) || testDate.after(endDate));
}

Doesn't seem that awkward to me. Note that I wrote it that way instead of

return testDate.after(startDate) && testDate.before(endDate);

so it would work even if testDate was exactly equal to one of the end cases.

Checking if date is within a date range

you are asking the date to be >= 5-days-time and <= 5-days-time. So unless it == 5-days-time it'll return false. I think you mean this:

DateTime dt = new DateTime();
if (DateTime.TryParse(c.Text, out dt))
{
DateTime now = DateTime.Now;
if (dt.Date >= now.Date && dt.Date <= now.AddDays(Mod.ValidUntilDays).Date)
{
e.Item.Style.Add("background-color", "#FF0303");
}
}

How to check if a time is within a range with date

It sounds like the easiest solution would be to get rid of the dates altogether and just compare hour values on their own. If a given time is greater than than 7 AM but less than 11 PM, tarrif = 3, else tarrif = 6. You can do this using the datetime object's hour property:

from datetime import datetime

cur_date = datetime.strptime('2/9/16 07:00', "%m/%d/%y %H:%M")
if 7 <= cur_date.hour < 23:
tarrif = 3
else:
tarrif = 6

For the sake of exactness, I'm assuming the tarrif changes from 6 to 3 at 7AM on the dot, and back to 6 at 11PM on the dot.

How to check if a date is within a certain range?

Simple solution using floor division:

start = datetime.date(day=7, month=9, year=2021)
timenow = datetime.date.today()
week = (timenow - start).days // 7 + 1

Check if a date within in range

Change:

var endDate = today;

to:

var endDate = new Date(today);

See the posts here for how objects are referenced and changed. There are some really good examples that help explain the issue, notably:

Instead, the situation is that the item passed in is passed by value.
But the item that is passed by value is itself a reference.

JSFiddle example



Related Topics



Leave a reply



Submit