Check current time is between two times
You may use datetime.weekday()
method and attributes: datetime.hour
and datetime.minute
.
now = datetime.datetime.now()
if now.weekday() < 5 and now.hour == 18 and 0 <= now.minute <= 15:
do_something()
Find out if current time is between two times
Perhaps you want something like this:
current_time = Time.now
open_time = store.open_time
close_time = store.close_time
current_time -= current_time.beginning_of_day
open_time -= open_time.beginning_of_day
close_time -= close_time.beginning_of_day
if current_time.between?(open_time, close_time)
puts "IN BETWEEN"
end
or
current_time = Time.now
open_time = store.open_time
close_time = store.close_time
current_time = [current_time.hour, current_time.min, current_time.sec]
open_time = [open_time.hour, open_time.min, open_time.sec]
close_time = [close_time.hour, close_time.min, close_time.sec]
if open_time <=> current_time == -1 and current_time <=> close_time == -1
puts "IN BETWEEN"
end
Check if time is between two values with hours and minutes in javascript
If its 14:04
your condition will fail as 4
is smaller 5
. The simplest would probably be to just take the full minutes of the day:
const start = 13 * 60 + 5;
const end = 19 * 60 + 57;
const date = new Date();
const now = date.getHours() * 60 + date.getMinutes();
if(start <= now && now <= end)
alert("in time");
How to know if current time is between two timespans?
You can just use:
if (startTime < now && now < endTime)
Note that:
- This doesn't check the date; doesn't look like that's an issue here
- Depending on why you're doing this, you may want to consider intervals such as "10pm-2am" at which point you effectively want to reverse the logic
In most cases, it's worth making the lower-bound inclusive and the upper-bound exclusive, e.g.
if (startTime <= now && now < endTime)
That's useful because then you can have several intervals where the end of one interval is the start of the next interval, and any one time is in exactly one interval.
To handle the "10pm-2am" example, you'd want something like:
if (interval.StartTime < interval.EndTime)
{
// Normal case, e.g. 8am-2pm
return interval.StartTime <= candidateTime && candidateTime < interval.EndTime;
}
else
{
// Reverse case, e.g. 10pm-2am
return interval.StartTime <= candidateTime || candidateTime < interval.EndTime;
}
Swift 5: Calculate current time is between 2 times
Set up the DateComponentsFormatter
let dcf = DateComponentsFormatter()
dcf.allowedUnits = [.day, .hour, .minute, .second]
dcf.unitsStyle = .full
Set up ordinary date formatter
let df = ISO8601DateFormatter()
df.formatOptions = [.withFullDate, .withDashSeparatorInDate]
Perform formatting
if let future = df.date(from: "2019-04-29"), let diff = dcf.string(from: Date(), to: future) {
print(diff)
}
Output is
3 days, 6 hours, 9 minutes, 9 seconds
Swfit- Check whether current time is between two time string
You can set the date formatter defaultDate
for today, parse the date strings, create a DateInterval
with the start and end date and check if it contains now Date()
:
extension Formatter {
static let today: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.locale = .init(identifier: "en_US_POSIX")
dateFormatter.defaultDate = Calendar.current.startOfDay(for: Date())
dateFormatter.dateFormat = "hh:mm a"
return dateFormatter
}()
}
func checkIfCurrentTimeIsBetween(startTime: String, endTime: String) -> Bool {
guard let start = Formatter.today.date(from: startTime),
let end = Formatter.today.date(from: endTime) else {
return false
}
return DateInterval(start: start, end: end).contains(Date())
}
let startTime = "10:30 AM"
let endTime = "06:30 PM"
checkIfCurrentTimeIsBetween(startTime: startTime, endTime: endTime) // true
Related Topics
Restarting a Loop from the Top
Is There Any Difference Between the ':Key =≫ "Value"' and 'Key: "Value"' Hash Notations
How to Dynamically Create a Local Variable
Ssl_Connect Returned=1 Errno=0 State=Sslv3 Read Server Certificate B: Certificate Verify Failed
Why Is "Slurping" a File Not a Good Practice
Why Are Exclamation Marks Used in Ruby Methods
How to Remove Rvm (Ruby Version Manager) from My System
Understanding the Rails Authenticity Token
How to Call Methods Dynamically Based on Their Name
How to Use the Conditional Operator (? :) in Ruby
How to Understand Nil Vs. Empty Vs. Blank in Ruby
Difference Between "Or" and || in Ruby
Ssl Error When Installing Rubygems, Unable to Pull Data from 'Https://Rubygems.Org/