How to Check If a Date Is in a Given 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.

How can i check if date is on range on Python?

You are comparing strings. You should compare datetime/date objects

import datetime
TODAY_CHECK = datetime.datetime.now()
start = datetime.datetime.strptime("26-11-2017", "%d-%m-%Y")
end = datetime.datetime.strptime("30-11-2017", "%d-%m-%Y")
if start <= TODAY_CHECK <= end:
print "PASS!"
else:
print "YOU SHALL NOT PASS, FRODO."

or you can do

start = datetime.datetime(day=26,month=11,year=2017)
end = datetime.datetime(day=30,month=11,year=2017)

Check if input date is in given range

If I am reading your code correctly, your date format is days/month/year which is not valid format. You need to swap the month and days.

var parts = d1.split(),
dateStr = parts[1] + "/" + parts[0] + "/" parts[2],
d1 = new Date(d1),
d = new Date();
d.setMonth(d.getMonth() - 6);
if(d1 > d ) {
console.log("ok");
} else {
console.log("error");
}

Query to check if a given range of date doesn't fits between multiple range of dates

Here is a simple way to check for date overlap in an insert query

insert into mytable(startDate, endDate)
select i.startDate, i.endDate
from (select '2019-12-23' startDate, '2019-12-30' endDate) i
where not exists (
select 1
from mytable t
where t.startDate <= i.endDate and t.endDate >= i.startDate
)

The date range to insert is declared in the subquery aliased i. If any record in the table overlaps with that range, the insert is skipped, else it happens.

Demo on DB Fiddle:

-- set up
CREATE TABLE mytable(
id int auto_increment primary key
,startDate DATE NOT NULL
,endDate DATE NOT NULL
);
INSERT INTO mytable(startDate,endDate) VALUES ('2019-12-10','2019-12-15');
INSERT INTO mytable(startDate,endDate) VALUES ('2019-12-16','2019-12-22');
INSERT INTO mytable(startDate,endDate) VALUES ('2019-12-29','2019-01-05');
INSERT INTO mytable(startDate,endDate) VALUES ('2020-01-20','2020-01-25');

-- initial table content
select * from mytable order by startDate

id | startDate | endDate
-: | :--------- | :---------
1 | 2019-12-10 | 2019-12-15
2 | 2019-12-16 | 2019-12-22
3 | 2019-12-29 | 2019-01-05
4 | 2020-01-20 | 2020-01-25
-- this range does not overlap
insert into mytable(startDate, endDate)
select i.startDate, i.endDate
from (select '2019-12-23' startDate, '2019-12-30' endDate) i
where not exists (
select 1
from mytable t
where t.startDate <= i.endDate and t.endDate >= i.startDate
)

-- confirm it was inserted
select * from mytable order by id

id | startDate | endDate
-: | :--------- | :---------
1 | 2019-12-10 | 2019-12-15
2 | 2019-12-16 | 2019-12-22
3 | 2019-12-29 | 2019-01-05
4 | 2020-01-20 | 2020-01-25
5 | 2019-12-23 | 2019-12-30
-- this range overlaps
insert into mytable(startDate, endDate)
select i.startDate, i.endDate
from (select '2019-12-23' startDate, '2019-12-28' endDate) i
where not exists (
select 1
from mytable t
where t.startDate <= i.endDate and t.endDate >= i.startDate
)

-- it was not inserted
select * from mytable order by id

id | startDate | endDate
-: | :--------- | :---------
1 | 2019-12-10 | 2019-12-15
2 | 2019-12-16 | 2019-12-22
3 | 2019-12-29 | 2019-01-05
4 | 2020-01-20 | 2020-01-25
5 | 2019-12-23 | 2019-12-30

Check if month or day or year is in range of two dates

You can create your own method to check if your date is valid of not, in your problem, you have three different cases, date which have only year, or date which have only year and month, or a full date, in this cases, you have a small problem when you want to parse the only the year, I can gives you this solution :

public static boolean isValid(String date, Instant start, Instant end) {
LocalDate sld = start.atOffset(ZoneOffset.UTC).toLocalDate();
LocalDate eld = end.atOffset(ZoneOffset.UTC).toLocalDate();
try {
LocalDate ld = LocalDate.parse(date);
return ld.isAfter(sld) && ld.isBefore(eld);
} catch (DateTimeParseException e) {}

try {
YearMonth ym = YearMonth.parse(date);
return ym.isAfter(YearMonth.from(sld)) && ym.isBefore(YearMonth.from(eld));
} catch (DateTimeParseException e) {}

try {
Year y = Year.parse(date);
return y.isAfter(Year.from(sld)) && y.isBefore(Year.from(eld));
} catch (DateTimeParseException e) {}
return false;
}

Here is an
Idemo demo



Related Topics



Leave a reply



Submit