How to change date format in rails - ruby-on-rails

Is this normal that rails put something like this :
DateTime.now = 2013-07-28T16:21:13+02:00
Why this T is between date and time ? How can i remove it. In I18n i have default:
default: ! '%a, %d %b %Y %H:%M:%S %z'

In your IRB console, if you call puts variable, it will make an implicit call to the method to_s on the variable object:
1.9.3 > DateTime.now
# => Wed, 28 Aug 2013 10:39:30 -0400
1.9.3 > puts DateTime.now
2013-08-28T10:39:33-04:00
# => nil
1.9.3 > DateTime.now.to_s
# => "2013-08-28T10:39:37-04:00"
This is why you see a "T" in the output, its .to_s's fault!

Related

Ruby and Rails "Date.today" format

In IRB, if I run following commands:
require 'date'
Date.today
I get the following output:
=> #<Date: 2015-09-26 ((2457292j,0s,0n),+0s,2299161j)>
But in Rails console, if I run Date.today, I get this:
=> Sat, 26 Sep 2015
I looked at Rails' Date class but can't find how Rails' Date.today displays the output differently than Ruby's output.
Can anybody tell, in Rails how Date.today or Date.tomorrow formats the date to display nicely?
The answer to your question is ActiveSupport's core extension to Date class. It overrides the default implementations of inspect and to_s:
# Overrides the default inspect method with a human readable one, e.g., "Mon, 21 Feb 2005"
def readable_inspect
strftime('%a, %d %b %Y')
end
alias_method :default_inspect, :inspect
alias_method :inspect, :readable_inspect
Command line example:
ruby-2.2.0 › irb
>> require 'date'
=> true
>> Date.today
=> #<Date: 2015-09-27 ((2457293j,0s,0n),+0s,2299161j)>
>> require 'active_support/core_ext/date'
=> true
>> Date.today
=> Sun, 27 Sep 2015
Rails' strftime or to_s methods should do what you need.
For example, using to_s:
2.2.1 :004 > Date.today.to_s(:long)
=> "September 26, 2015"
2.2.1 :005 > Date.today.to_s(:short)
=> "26 Sep"
If you run this:
require 'date'
p Date.today.strftime("%a, %e %b %Y")
You'll get this: "Sat, 26 Sep 2015"

Rails translate current date in another format

In Rails I'm trying to localize the date:
2.1.1 :005 > Date.today
=> Mon, 14 Apr 2014
2.1.1 :006 > I18n.localize(Date.today)
=> "14/04/2014"
2.1.1 :007 >
The second output is not the correct translation of the first!
Can you help me ?
You can define a new format:
en:
date: # there is also a section for datetime and time
formats:
day_month_abbr: "%a, %d %b %Y"
and use it like this:
I18n.localize(Date.today, format: :day_month_abbr)
# => "Mon, 14 Apr 2014"
Or you can overwrite the default format:
en:
date:
formats:
default: "%a, %d %b %Y"
And then you don't need to give any argument:
I18n.l(Date.today) #=> "Mon, 14 Apr 2014"
List of all the wildcards usable for DateTime/Time/Date here: http://apidock.com/ruby/DateTime/strftime
The second upload is in fact the correct translation.
If you want to customize the output formatting, check out the documentation here:
http://edgeguides.rubyonrails.org/i18n.html#adding-date-time-formats

Ruby parse time zone string FORMAT for DB

I am trying to parse a string that's hitting my api. The incoming string is
"2014-03-19T04:00:00.000Z"
I need it in the following format for my db sql to work:
"2014-03-19 00:00:00 -0400"
Right now, the solution I have come up with is
Time.zone.parse("2014-03-19T04:00:00.000Z").in_time_zone('America/New_York').to_s
This feels like an inelegant solution to me and I feel that there should be a more dynamic way of doing things without specifying the time zone name (it should be local by default). I just want to switch the formatting of the strings as they are supposed to be equivalent.
Thanks
Configure the time_zone in the application.rb file and use it as below:
# application.rb:
class Application < Rails::Application
config.time_zone = 'America/New_York'
end
Time.zone.parse("2014-03-19T04:00:00.000Z").to_s
# => "2014-03-19 00:00:00 -0400"
More examples of use
$ > Time.zone = "America/New_York"
# => "America/New_York"
$ > Time.zone
# => (GMT-05:00) America/New_York
$ > Time.zone.now
# => Sat, 22 Mar 2014 18:28:48 EDT -04:00
irb(main):008:0> Time.parse("2014-03-19T04:00:00.000Z").getlocal.strftime("%F %T %z")
=> "2014-03-19 04:00:00 +0000"
On a system in GMT+1:
irb(main):003:0> Time.parse("2014-03-19T04:00:00.000Z").getlocal.strftime("%F %T %z")
=> "2014-03-19 05:00:00 +0100"

Rails not picking up custom date & time formats in en.yml

I'm not that familiar with I18N in Rails so bear with me. Trying to set a custom date & time format:
#config/locales/en.yml
en:
date:
formats:
long_dateweek: "%A, %B %d, %Y"
time:
formats:
very_short: "%H:%M"
But when I try to use them I just get the default formats:
1.9.3p194 :001 > Time.now.to_date.to_s(:long_dateweek)
=> "2012-08-22"
1.9.3p194 :002 > Time.now.to_time.to_s(:very_short)
=> "2012-08-22 16:12:47 -0700"
Tried restarting console (and even the server) to no avail... What'd I miss?
You need to use the I18n.l method as follows:
1.9.3p194 :001 > I18n.l Time.now.to_date, :format => :long_dateweek
=> "Wednesday, August 22, 2012"
1.9.3p194 :002 > I18n.l Time.now, :format => :very_short
=> "23:03"
You can also use the l helper method in your views. Look at this rails guide for more information.
#alexsanford1 has the right answer. I modified mine below to work in the view.
<%= l Time.now, :format => :very_short %>

Ruby on Rails 3 Time - Parse Milliseconds

I'm trying to parse a date like: 2011-05-21 04:20:46.011 into a Time object in Rails. So far I have the following:
Time.strptime("2011-05-21 04:20:46.011", "%Y-%m-%d %H:%M:%S.%3N")
But it doesn't seem to like the "%3N" at the end. How do I parse this date?
Use parse:
x = Time.parse("2011-05-21 04:20:46.011", "%Y-%m-%d %H:%M:%S.%3N")
# => 2011-05-21 04:20:46 -0700
x.usec
# => 11000
In many case you don't need to pass in the format either:
x = Time.parse("2011-05-21 04:20:46.011")
# => 2011-05-21 04:20:46 -0700
x.usec
# => 11000
Try Time.parse("2011-05-21 04:20:46.011", "%Y-%m-%d %H:%M:%S.%3N")
With Ruby v 2.3.2 and Rails v 5.0.0.1 in rails console
2.3.2 :035 > Time.parse("2011-05-21 04:20:46.011", "%Y-%m-%d %H:%M:%S.%3N")
NoMethodError: undefined method `getlocal' for "%Y-%m-%d %H:%M:%S.%3N":String
from /home/jignesh/.rvm/rubies/ruby-2.3.2/lib/ruby/2.3.0/time.rb:264:in `make_time'
from /home/jignesh/.rvm/rubies/ruby-2.3.2/lib/ruby/2.3.0/time.rb:366:in `parse'
from (irb):35
Note: In code below in the parse format, NOT prefixed %N with the number of fractional digits like %3N and instead simply specified %N and it works
2.3.2 :037 > Time.strptime("2011-05-21 04:20:46.011", "%Y-%m-%d %H:%M:%S.%N")
=> 2011-05-21 04:20:46 +0530
2.3.2 :038 > tt = Time.strptime("2011-05-21 04:20:46.011", "%Y-%m-%d %H:%M:%S.%N")
=> 2011-05-21 04:20:46 +0530
2.3.2 :039 > tt.usec
=> 11000
The above code didn't worked when using parse
2.3.2 :040 > tt = Time.parse("2011-05-21 04:20:46.011", "%Y-%m-%d %H:%M:%S.%N")
NoMethodError: undefined method `getlocal' for "%Y-%m-%d %H:%M:%S.%N":String
from /home/jignesh/.rvm/rubies/ruby-2.3.2/lib/ruby/2.3.0/time.rb:264:in `make_time'
from /home/jignesh/.rvm/rubies/ruby-2.3.2/lib/ruby/2.3.0/time.rb:366:in `parse'
from (irb):40
It looks strange because Time.strptime(date, format, now=self.now) official documentation does mention about %3N, %6N and %9N explicitly and using them in the parse format doesn't work!
Rails does provide a counterpart strptime(str, format, now=now()) as part of ActiveSupport::TimeZone API and it internally does the parsing using Ruby's standard DateTime._strptime as shown in its source-code:
# File activesupport/lib/active_support/values/time_zone.rb, line 382
def strptime(str, format, now=now())
parts_to_time(DateTime._strptime(str, format), now)
end
And I guess DateTime._strptime internally delegates to Ruby's standard Time.strptime
For those who are looking for leveraging Rails's Time.zone.parse counterpart for parsing in a specific format they can use below code:
tt = Time.zone.strptime('1999-12-31 14:00:00.011111', '%Y-%m-%d %H:%M:%S.%N')
=> Fri, 31 Dec 1999 14:00:00 UTC +00:00
2.3.2 :031 > tt.usec
=> 11111
Hope someone from the core-team can clarify about this behaviour observed and if the behaviour is normal then at-least a note should be made about in the documentation.

Resources