How to know if today's date is in a date range?
Use ===
Actually, there is an operator that will do this. Make a Range
and compare Time
objects to it using the ===
operator.
start = Time.now.to_i
range = start..(start + 2)
inside = start + 1
outside = start + 3 # ok, now...
range === inside # true
range === outside # false
Update post-comment-flood: This version works well everywhere. (In Rails, in Ruby 1, and in Ruby 2.) The earlier
irb
example also worked fine but the interactive example wasn't always reproduced correctly in some experiments. This one is easier to cut-and-paste.It's all straightened out now.
Check if one date is between two dates
Date.parse
supports the format mm/dd/yyyy
not dd/mm/yyyy
. For the latter, either use a library like moment.js or do something as shown below
var dateFrom = "02/05/2013";
var dateTo = "02/09/2013";
var dateCheck = "02/07/2013";
var d1 = dateFrom.split("/");
var d2 = dateTo.split("/");
var c = dateCheck.split("/");
var from = new Date(d1[2], parseInt(d1[1])-1, d1[0]); // -1 because months are from 0 to 11
var to = new Date(d2[2], parseInt(d2[1])-1, d2[0]);
var check = new Date(c[2], parseInt(c[1])-1, c[0]);
console.log(check > from && check < to)
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)
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));
}
PHP check if date between two dates
Edit: use
<=
or>=
to count today's date.
This is the right answer for your code. Just use the strtotime() php function.
$paymentDate = date('Y-m-d');
$paymentDate=date('Y-m-d', strtotime($paymentDate));
//echo $paymentDate; // echos today!
$contractDateBegin = date('Y-m-d', strtotime("01/01/2001"));
$contractDateEnd = date('Y-m-d', strtotime("01/01/2012"));
if (($paymentDate >= $contractDateBegin) && ($paymentDate <= $contractDateEnd)){
echo "is between";
}else{
echo "NO GO!";
}
Checking if a given date fits between a range of dates
With SQL Server it's actually as simple as:
SELECT startDate, endDate
FROM YourTable
WHERE '2012-10-25' between startDate and endDate
Related Topics
Ruby Timeouts and System Commands
How to Calculate How Many Years Passed Since a Given Date in Ruby
Invalid Gemspec -Illformed Requirement ["#<Yaml::Syck::Defaultkey:0Xb5F9C990> 3.2.0"]
How to Deal with Memory Leaks in Rmagick in Ruby
Ruby Gets/Puts Only for Strings
How to Have Rspec Test for My Default Scope
How to Find Each Instance of a Class in Ruby
Parse Email Addresses for "From" and "To" Fields in Ruby
What Does ::Myclass Ruby Scope Operator Do
Net::Ssh Sudo Command Hangs After Entering Password
Regex, How to Match Multiple Lines
Yaml Indentation for Array in Hash
Ruby Method That Returns Itself
Ruby Class Inheritance: What Is '<<' (Double Less Than)
How to Click First Link in List of Items After Upgrading to Capybara 2.0
Determining If a Variable Is Within Range
What Is the Best Practice When It Comes to Testing "Infinite Loops"