check time between 9.30 to 4 ruby
t = Time.now
Range.new(
Time.local(t.year, t.month, t.day, 9),
Time.local(t.year, t.month, t.day, 16, 30)
) === t
Time range method not working in Rails 4/Ruby 2 app
I would take advantage of the fact that DateTime objects support comparison. E.g.
Time.local(2014, 12, 31, 6, 30) > Time.local(2015, 1, 1, 6, 30)
=> false
Hence you could rewrite the method during_business_hours
:
# Check if time `t` is between 6:30 and 21:00 of `t`'s day, a.k.a. in between business hours.
# If that's the case, return true, otherwise false.
def during_business_hours(t)
if t < Time.local(t.year, t.month, t.day, 6, 30) ||
t > Time.local(t.year, t.month, t.day, 21)
return false
else
return true
end
end
Personally, your current code for that method is very hard to read, the new version is much clearer.
...and BTW, I would make it a private
method.
def validate_each [snip]
private
def during_business_hours(t)
[snip]
Check if current datetime is between 08 PM and 08 AM and calculate total hours left until 08 AM
Here is one simple way to do it:
var date = DateTime.Now;
if(date.Hour >= 20 || date.Hour < 8)
{
var DateTime8 = date.Date.AddHours((date.Hour > 8) ? 24 + 8 : 8);
TimeSpan diff = DateTime8 - date;
}
Custom Validator to Prevent an appointment from being scheduled too early in day?
You can create your own, the same way active record does. This results in clean, portable, easy to test custom validations. An example might look like:
create app/validators/during_business_hours_validator.rb with:
class DuringBusinessHoursValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
# example - do whatever you want here
unless value.present? && during_business_hours(value)
record.errors[attribute] << 'must be during business hours'
end
end
def during_business_hours(time)
# from http://stackoverflow.com/q/10090962/525478
Range.new(
Time.local(time.year, time.month, time.day, 6, 30),
Time.local(time.year, time.month, time.day, 21)) === time
end
end
then, in your model add:
validates :appointment_date,
during_business_hours: true
PHP - compare variables to see if between specified start and end time
I'm not sure with that kind of time format.. maybe it is best if you first convert it on something that can be easily read by php.
try using strtotime() or do some manual conversion.
Generate array of times (as strings) for every X minutes in JavaScript
If the interval is only to be set in minutes[0-60], then evaluate the below solution w/o creating the date object and in single loop:
var x = 5; //minutes intervalvar times = []; // time arrayvar tt = 0; // start timevar ap = ['AM', 'PM']; // AM-PM
//loop to increment the time and push results in arrayfor (var i=0;tt<24*60; i++) { var hh = Math.floor(tt/60); // getting hours of day in 0-24 format var mm = (tt%60); // getting minutes of the hour in 0-55 format times[i] = ("0" + (hh % 12)).slice(-2) + ':' + ("0" + mm).slice(-2) + ap[Math.floor(hh/12)]; // pushing data in array in [00:00 - 12:00 AM/PM format] tt = tt + x;}
console.log(times);
Rails Rounding float with different options
Here's a generic way to do it for any precision:
class Float
def round_currency(precision: 1, direction: :none)
round_method = case direction
when :none then :round
when :up then :ceil
when :down then :floor
end
integer_value = (self * 100).round
((integer_value / precision.to_f).send(round_method) * precision / 100.0)
end
end
# USAGE
9.37.round_currency(direction: :none, precision: 10)
# => 9.4
9.37.round_currency(direction: :up, precision: 25)
# => 9.5
9.37.round_currency(direction: :none)
# => 9.37
# Precision is defined in pennies: 10 dime, 25 quarter, 100 dollar. 1 penny is default
This code converts the float into an integer first to ensure accuracy. Be wary using ceil
and floor
with floating number arithmetic - due to accuracy errors you could get odd results e.g. 9.37 * 100 = 936.9999999999999
. If you floor
the result, you'll end up rounding to 9.36
Related Topics
If Else Statements in .Html.Erb in Views
Creating a Hash with Values as Arrays and Default Value as Empty Array
Are There Better Ways to Prevent 'Yield' When No Block Is Passed In
Missing a Template for This Request Format and Variant
Rails Before_Filter for Specific Actions in Controller
Accepts_Nested_Attributes_For Rails 4 Is Not Deleting
Verb-Agnostic Matching in Sinatra
How to Run Ruby 2.0 with Jruby 1.7
What Is a Worker in Ruby/Rails
How to Force One Field in Ruby's CSV Output to Be Wrapped with Double-Quotes
How to Find a Model's Relationships
Using the Value of a Variable as Another Variables Name in Ruby
How to Generate Coordinates in Between Two Known Points
Rendering Haml Partials from Within Haml Outside of Rails
How to Highlight Fields on Rails Validation Errors
Advantages and Disadvantages of Ruby on Rails Polymorphic Relationships
Printing to Screen in a Rake Task
Are Ruby 1.9 Regular Expressions Equally Powerful to a Context Free Grammar