I tried to parse this time string "21:58:06 Apr 29, 2015 PDT". What is the right way to do that?
first approach:
zone = Time.zone
Time.zone = "Pacific Time (US & Canada)"
payed_at = Time.parse params[:payment_date]
payed_at.in_time_zone(zone)
result:
Thu, 30 Apr 2015 10:21:30 CEST +02:00
second guess:
payed_at = DateTime.parse date
result:
Thu, 30 Apr 2015 21:58:29 +0000
the correct result would be
Thu, 30. April 2015, 06:58 Uhr
environment:
ruby '2.1.5'
gem 'rails', '~> 3.2.15'
Since you have rails specified in tags, here is an ActiveSupported solution:
tz = ActiveSupport::TimeZone['US/Pacific']
tz.parse("21:58:06 Apr 29, 2015 PDT").localtime
#⇒ 2015-05-01 06:58:29 +0200
Parse using strptime method
Eg : DateTime.strptime('December 09, 2011', '%B %d, %Y')
http://ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/DateTime.html#method-c-_strptime
Related
I am using the helpful gem timecop (https://github.com/travisjeffery/timecop) for my tests with rspec and today an old unchanged test is breaking.
I might be mistaken in the way I am using it but using pry I printed the following:
Time.zone.now => Wed, 31 Mar 2021 15:09:45 CEST +02:00
6.months.from_now => Thu, 30 Sep 2021 15:09:56 CEST +02:00
Timecop.travel(6.months.from_now)
Time.zone.now => Thu, 30 Sep 2021 15:10:10 CEST +02:00
6.months.ago => Tue, 30 Mar 2021 15:10:15 CEST +02:00
Thank you in advance for any idea to understand or solve this.
Have a nice day.
Ok it is my mistake, there is no 31st in September so it falls back to 30th and then 6 months ago is 30th of March.
I've been banging my head against this for a while and I can't seem to understand how rails timezones and in_time_zone works.
Here is some rails c output that I'd like to understand:
[26] VMM(bby - main - dev)> Time.zone.now
=> Wed, 14 Mar 2018 09:13:17 CDT -05:00
[27] VMM(bby - main - dev)> MyModel.first.started_at
=> Fri, 09 Mar 2018 09:17:00 CST -06:00
[28] VMM(bby - main - dev)> MyModel.first.started_at.in_time_zone(Time.zone)
=> Fri, 09 Mar 2018 09:17:00 CST -06:00
So:
From the first line, the Time.zone seems to be CDT -5.
From the second line, the started_at attribute seem to be CST -6
On the third line, my intention is to change that atribute to use CDT -5, so I'd expect an output of Fri, 09 Mar 2018 10:17:00 CDT -05:00
Why does this behave as it does instead of how I expect it to?
Thanks in advance!
I have date as "Wed, 29 Jun 2016" and time in "11:35 PM" format
how can i create a date time object with it?
something like what Time.current does.
It's usually pretty simple if your date can be parsed:
DateTime.parse("Wed, 29 Jun 2016 11:35 PM")
# => Wed, 29 Jun 2016 23:35:00 +0000
You can then use that in any capacity you'd use any other date/time.
Why you don't use next:
Time.parse('Wed, 29 Jun 2016 11:35 PM')
=> 2016-06-29 23:35:00 +0300
I received a time string from a server and I want to parse or convert that string to configured time zone (Tokyo timezone in my case):
Input: "2016-05-27T09:00:00.0000000"
Expected output: Fri, 27 May 2016 18:00:00 JST +09:00
I try to type like this: Time.zone.parse("2016-05-27T09:00:00.0000000") but it returns unexpected output: Fri, 27 May 2016 09:00:00 JST +09:00
If the parsed datetime is a UTC time, add the UTC timezone explicitly to it before parsing:
# this parses the time as local time:
Time.zone.parse("2016-05-27T09:00:00.0000000")
# => Fri, 27 May 2016 09:00:00 JST +09:00
# this parses the time as UTC and converts to local time:
Time.zone.parse("2016-05-27T09:00:00.0000000Z")
# => Fri, 27 May 2016 18:00:00 JST +09:00
Note the "Z" appended to the datetime string, meaning that this is a datetime in UTC timezone.
how can I get a variable which is holding always Today midnight in my timezone?
The hosting server is several hours behind me, both Time.now.midnight and Date.today are on yesterday date for good part of the day.
Thanks
Found the solution.
now=DateTime.now
=> Fri, 03 Feb 2012 21:57:21 EST -05:00
now.in_time_zone('London').midnight
=> Sat, 04 Feb 2012 00:00:00 GMT +00:00
now.in_time_zone('Hawaii').midnight
=> Fri, 03 Feb 2012 00:00:00 HST -10:00