Convert DateTime String to UTC in rails
Time.parse("2010-01-01 12:30:00").getutc
EDIT
(grinding teeth while thinking about the nightmare which is Ruby/Rails date/time handling)
OK, how about this:
Time.zone.parse("2010-01-01 12:30:00").utc
Note that Time.zone.parse
returns a DateTime
, while appending the .utc
gives you a Time
. There are differences, so beware.
Also, Time.zone is part of Rails (ActiveSupport), not Ruby. Just so you know.
Converting a UTC date string into a localized date string in Ruby or Rails
Time.zone.parse("2019-09-15T20:42:15")
.in_time_zone("America/Los_Angeles")
.strftime("%Y-%m-%d %H:%M:%S")
# => "2019-09-15 13:42:15"
See ActiveSupport::TimeWithZone for the ActiveSupport extension to Rubys time and date objects and ActiveSupport::TimeZone for a list of time zone mappings.
But for caching reasons you should consider localizing dates and times on the client side.
Converting datetime with one timezone to utc
Using the strptime
method on the Rails DateTime
class, you can parse a DateTime
object from a string containing both the time and timezone (timezone is passed via the %z
directive). From there, you can convert the time to UTC:
a = "04/23/2014 04:00"
b = "Eastern Time (US & Canada)"
datetime_with_tz = DateTime.strptime([a, b].join(' '), "%m/%d/%Y %H:%M %z")
#=> Wed, 23 Apr 2014 04:00:00 -0500
datetime_with_tz.utc
#=> Wed, 23 Apr 2014 09:00:00 +0000
Convert DateTime String with TimeZone to the DateTime with TimeZone Or in UTC in rails 3
Is this what you're looking for?
DateTime.parse("2012-06-22T21:39:48-05:00").utc
Or am I missing something?
How do I Convert DateTime.now to UTC in Ruby?
d = DateTime.now.utc
Oops!
That seems to work in Rails, but not vanilla Ruby (and of course that is what the question is asking)
d = Time.now.utc
Does work however.
Is there any reason you need to use DateTime
and not Time
? Time
should include everything you need:
irb(main):016:0> Time.now
=> Thu Apr 16 12:40:44 +0100 2009
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
Related Topics
Cannot Install Aptana Plugin on Eclipse 4.2
Rails: Ensure Only One Boolean Field Is Set to True at a Time
How to Convert an Existing Rails 3 Application into an Engine
No Implicit Conversion of String into Integer (Typeerror)
Rails: Respond_To JSON and HTML
How Does Rails Implement Before_Filter
Convert String to Decimal Number in Ruby
Ruby Merging Two Arrays into One
Asserting That a Particular Exception Is Thrown in Cucumber
Why Are Symbols Not Frozen Strings
Namespacing Thor Commands in a Standalone Ruby Executable
Generating Unique Token on the Fly with Rails
How Might I Pass Text Data from the Ruby Console into My Clipboard Without Saving to a File
How to Call Applicationcontroller Methods from Applicationhelper
Rails Belongs_To Association (With :Class_Name) Returns Nil