Hello,
I have an application which stores date in DateTime format, and displays it as long format, for example: November, 4, 2009
I want to extract month name and year only from the DateTime format to display it as: November 2009.
Any help?
Hello,
I have an application which stores date in DateTime format, and displays it as long format, for example: November, 4, 2009
I want to extract month name and year only from the DateTime format to display it as: November 2009.
Any help?
Hi Ahmed Abdelsalam
Read this
http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Date/Conversions.html
Sijo
Check out strftime in the API.
shyam mohan wrote:
use this
show_time = Time.now.strftime("%B %Y")
Regards, Shyam +91-971-618-9650 shyam@crypsis.net
Thank you very much. It works.
Ahmed Abdelsalam wrote:
I want to extract month name and year only from the DateTime format to display it as: November 2009.
FYI: If you find yourself using the same custom date/time format in multiple places in your application you can DRY that up by adding to the named date/time formats using:
ActiveSupport::CoreExtensions::Conversions::DATE_FORMATS.merge!(:concise
=> "%d.%b.%y")
Ahmed Abdelsalam wrote:
I want to extract month name and year only from the DateTime format to display it as: November 2009.
FYI: If you find yourself using the same custom date/time format in multiple places in your application you can DRY that up by adding to the named date/time formats using:
ActiveSupport::CoreExtensions:
:Conversions::DATE_FORMATS.merge!(:concise => "%d.%b.%y")
You can shorten that up by creating config/initializers/time_formats.rb and dropping this into it:
Date::DATE_FORMATS[:concise] = Time::DATE_FORMATS[:concise] = "%d.%b.%y"
ARG! Yes, it works - but I don't think I'd call that "the rails way."
formatting dates and times is really part of internationalization - even if it doesn't feel like it in this case.
Check out http://guides.rubyonrails.org/i18n.html#adding-datetime-formats for what may be a more elegant solution. Certainly nicely centralized if you want to use that format in more than one location.
kwerle@pobox.com wrote:
ARG! Yes, it works - but I don't think I'd call that "the rails way."
formatting dates and times is really part of internationalization - even if it doesn't feel like it in this case.
Yes, this I know. Maybe I should have mentioned that.
I suppose I was thinking in BDD... "The simplest solution that could possibly work." I wouldn't consider implementing I18n based on date/time formats alone. However, once I had a need for I18n then date/time formats would certainly be re-factored into that implementation.
Hi Ahmed Abdelsalam
Read this
http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Date/Conversions.html
Sijo
For DateTime formatting, one should use the following document instead:
http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/DateTime/Conversions.html
-Conrad
Hello,
I have an application which stores date in DateTime format, and displays
it as long format, for example: November, 4, 2009
I want to extract month name and year only from the DateTime format to
display it as: November 2009.
Ahmed, you’ll need to do the following:
a) add the following to your config/initializers/time_formats.rb file:
Time::DATE_FORMATS[::month_and_year] = “%B %Y”
b) call it by doing
post.created_at.to_formatted_s(:month_and_year)
c) simply syntax in two by adding an instance method to the relevant
model file
def some_date_format
self.created_at.to_formatted_s(:month_and_year)
end
Good luck,
-Conrad
@Conrad Taylor - This is exactly how it should be done.
Going further, if You would need to change date/time format in whole application from one place You could use:
#config/enviroment.rb ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS[:default] = lambda { |time| I18n.l time, :format => :db }
Best, Martin