Convert 12 hr time to 24 hr format in Ruby
I would first parse the string with Time#strptime
and then output it with Time#strftime
. This ensures a strict check with your original format as well.
require 'time'
Time.strptime("10pm", "%I%P").strftime("%H:%M")
=> "22:00"
Converting a 12 hour time string with AM/PM to 24 hour time
Have you tried this?time = "12:00 PM"
Time.strptime(time, "%I:%M %P").strftime("%H:%M")
Convert 12-hour time to 24-hour time with a regex
You can take advantage of the fact that String#gsub
accepts a block. Something like this will do for you?
s = "Mon-Fri: AM7:00-PM8:00\nSat-Sun: AM8:00-PM11:00"
s2 = s.gsub('AM', '').gsub(/PM(\d+)/) do |match|
(match.gsub('PM', '').to_i + 12).to_s
end
s2 # => "Mon-Fri: 7:00-20:00\nSat-Sun: 8:00-23:00"
Convert 24 integer to 12 hour with am/pm
[14, 0, 23].map { |hour| Time.parse("#{hour}:00").strftime("%l %P") }
=> [" 2 pm", "12 am", "11 pm"]
Change 24 hour clock to 12 hour for created_at
Look up strftime
(for any language), and build the actual template yourself. Per Time::DATE_FORMATS
, the offending template is '%B %d, %Y %H:%M'
, so change the end to %I:%M %p
.
(You also might want to add a new format to DATE_FORMATS
...)
Convert to/from DateTime and Time in Ruby
You'll need two slightly different conversions.
To convert from Time
to DateTime
you can amend the Time class as follows:
require 'date'
class Time
def to_datetime
# Convert seconds + microseconds into a fractional number of seconds
seconds = sec + Rational(usec, 10**6)
# Convert a UTC offset measured in minutes to one measured in a
# fraction of a day.
offset = Rational(utc_offset, 60 * 60 * 24)
DateTime.new(year, month, day, hour, min, seconds, offset)
end
end
Similar adjustments to Date will let you convert DateTime
to Time
.
class Date
def to_gm_time
to_time(new_offset, :gm)
end
def to_local_time
to_time(new_offset(DateTime.now.offset-offset), :local)
end
private
def to_time(dest, method)
#Convert a fraction of a day to a number of microseconds
usec = (dest.sec_fraction * 60 * 60 * 24 * (10**6)).to_i
Time.send(method, dest.year, dest.month, dest.day, dest.hour, dest.min,
dest.sec, usec)
end
end
Note that you have to choose between local time and GM/UTC time.
Both the above code snippets are taken from O'Reilly's Ruby Cookbook. Their code reuse policy permits this.
Ruby on Rails convert date and time value properly formatted utc time
Thanks everyone! But, I could solve the problem on my own!
time = bid_end_time.hour.to_s + ":" + bid_end_time.min.to_s + ":" + bid_end_time.sec.to_s
date_time = bid_end_date.to_s + " " + time
bid_end_date_time = Time.zone.parse(date_time).utc
24 hours in minutes in ruby on rails
The most appropriate way to do this is probably:
24.hours / 1.minute
This way you keep the readability and clear intention of the 'ruby mindset'.
Of course, this will have a constant value so you should probably store it in an appropriately named constant (e.g. MINUTES_IN_A_DAY
) rather than calculating it every time.
Related Topics
Custom Rails Configuration Section
Sequel Accessing Many_To_Many Join Table When Adding Association
Rails Two-Legged Oauth Provider
Ruby Imap Idle Concurrency - How to Tackle
Sleep Until Condition Is True in Ruby
Rails Join a List of Strings with Commas and "And" Before the Last
How to Remove a Non-Breaking Space in Ruby
Ruby Escape Argv Argument or String as Argument to Shell Command
How to Enable Compression in Ruby on Rails
Require Lib in Rspec with Ruby 1.9.2 Brings "No Such File to Load"
How to Do Sti and Still Use Polymorphic Path Helpers
Where Does Ruby Keep Track of Its Open File Descriptors
How to Catch All Http Traffic (Local Proxy)
Ruby on Rails with Imap Idle for Multiple Accounts