I am faced with an issue in Ruby on Rails. I am looking to convert a string of format Tue, 10 Aug 2010 01:20:19 -0400 (EDT) to a date object.
Is there anyway i could do this.
Here is what I've looked and tried at the following with no luck:
Date.strptime(updated,"%a, %d %m %Y %H:%M:%S %Z")
Chronic Parser
Ruby: convert string to date
Parsing date from text using Ruby
Please help me out with this.
What is wrong with Date.parse method?
str = "Tue, 10 Aug 2010 01:20:19 -0400 (EDT)"
date = Date.parse str
=> #<Date: 4910837/2,0,2299161>
puts date
2010-08-10
It seems to work.
The only problem here is time zone. If you want date in UTC time zone, then it is better to use Time object, suppose we have string:
str = "Tue, 10 Aug 2010 01:20:19 +0400"
puts Date.parse str
2010-08-10
puts Date.parse(Time.parse(str).utc.to_s)
2010-08-09
I couldn't find simpler method to convert Time to Date.
Date.strptime(updated,"%a, %d %m %Y %H:%M:%S %Z")
Should be:
Date.strptime(updated, '%a, %d %b %Y %H:%M:%S %Z')
str = "Tue, 10 Aug 2010 01:20:19 -0400 (EDT)"
str.to_date
=> Tue, 10 Aug 2010
You can try https://rubygems.org/gems/dates_from_string:
Find date in structure:
text = "get car from repair 2015-02-02 23:00:10"
dates_from_string = DatesFromString.new
dates_from_string.find_date(text)
=> ["2015-02-02 23:00:10"]
Related
This question already has answers here:
Convert a string date format from "17-Nov-2011" to "11/17/11"
(3 answers)
Closed 5 years ago.
I'm learning Ruby on Rails and have dates with the format
20170802173300 witch is:
Mie, 02 Ago 2017 17:33:00 -0300.
How can I convert this to ?
2017-08-02 17:33:00 -0300
You can use strftime:
datetime = 'Mie, 02 Ago 2017 17:33:00 -0300'
datetime.strftime('%Y-%m-%d %H:%m:%S %z')
# => 2017-08-02 17:33:00
You can format time anyway you like, in Ruby with strftime.
For format you want it'll be like this:
%Y-%m-%d %H:%M:%S %z
For example: Time.now.strftime('%Y-%m-%d %H:%M:%S %z')
require 'date'
date = '20170802173300'
formatted_date = DateTime.parse(date).to_s
# => "2017-08-02T17:33:00+00:00"
# and to be more explicit:
really_formated_date = DateTime.parse(date).strftime('%Y-%m-%d %H:%M:%S %Z')
# => "2017-08-02 17:33:00 +00:00"
Date now = new Date();
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yy");
I'm trying to convert Active Support timezone original format into a string. I want to store it in an array of characters then parse each needed data individually.
Time.zone = current_user.timezone
date_and_time = Time.zone.now
Now
date_and_time = Thu, 21 Apr 2016 20:58:04 PDT -07:00
Ruby method ( to_s ) does not convert it. I found other ways to convert it to but all of them will change the format to numbers only, I want the day to stay the same because I will store it in a variable then use it in a different method.
You can use .to_formatted_s(DATE_FORMAT) for this.
time = Time.now # => Thu Jan 18 06:10:17 CST 2007
time.to_formatted_s(:db) # => "2007-01-18 06:10:17"
time.to_formatted_s(:long) # => "January 18, 2007 06:10"
time.to_formatted_s(:long_ordinal) # => "January 18th, 2007 06:10"
time.to_formatted_s(:rfc822) # => "Thu, 18 Jan 2007 06:10:17 -0600"
time.to_formatted_s(:iso8601) # => "2007-01-18T06:10:17-06:00"
A list of all DATE_FORMATS and more information can be found here:
http://api.rubyonrails.org/classes/Time.html#method-i-to_formatted_s
You can try this
date_and_time.strftime("%a %d %b %Y")
Also You can check this guide, to get format you want
You should get what you want using this :
date_and_time.strftime("%a %d %b %Y %H:%M:%S UTC %:z")
Please see strftime Docs for more info
Explanation
Reason for hardcoding UTC is so that according to the docs
%z - Time zone as hour and minute offset from UTC
So i believe it should be UTC all the time.
I'm trying to have my datetime object display a specific way.
I like how
event.mytimeobject.to_formatted_s(:rfc822)
displays as: "Mon, 24 Aug 2015 00:00:00 +0000"
Except for the "00:00:00 +0000"
I'm trying to have it display as: "Mon, 24 Aug 2015"
So far, I've tried creating a file, config/intializers/date_formats.rb, with the following code
Time::DATE_FORMATS[:default] = "%A, %e %b %Y"
Date::DATE_FORMATS[:default] = "%A %e %b %Y"
but nothing happens when I add
event.mytimeobject.to_formatted_s(:default)
I've also tried
event.mytimeobject.strftime(%A,%e %b %Y)
but I'm getting errors with that as well
Any ideas?
If I have #today = Date.today.to_s, how do I convert #today into UTC (with the appropriate date only)? But the format should be like this : 2011-03-08 00:00:00
Acutally I am looking for Yesterday date also ??
This worked for me to get yesterday at time 00hr
Date.yesterday.to_time.utc.at_beginning_of_day
=> Sun Mar 06 00:00:00 UTC 2011
I don't know if its the correct way to do it but it works
ruby-1.9.2-p0 > Date.today.to_time.utc
=> 2011-03-07 18:15:00 UTC
and for yesterday, you can subtract a day form today
(Date.today-1.day).to_time.utc
=> 2011-03-06 18:15:00 UTC
For Yesterday Date.
#date = Date.yesterday
=> Mon, 07 Mar 2011
#date.to_time.utc.strftime("%Y-%m-%d %H:%M:%S")
=> "2011-03-06 18:30:00"
This will do(%F - %Y-%m-%d and %T - %H:%M:%S)
Date.today.strftime("%F %T")
Date.yesterday.strftime("%F %T")
If you are particular about UTC time, you should do like this
(Time.now.utc).to_date.strftime("%F %T")
(Time.now.utc - 1.day).to_date.strftime("%F %T")
>> events.first.datetime
=> Wed Sep 15 19:00:00 -0400 2010
>> Time.parse(events.first.datetime)
NoMethodError: private method `gsub!' called for Wed Sep 15 19:00:00 -0400 2010:Time
Time#parse creates a Time object out of a String, which it takes as its first argument. You already have a Time object, so Time.parse doesn't know what to do with it.
In order to format the date like you want it, take a look at Time#strftime. You can format it like you want with the format string:
events.first.datetime.strftime("%A %B %d, %Y at %I:%M %p")
Take a look at the manual entry for strftime for other format specifiers.