DateTime format

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 A.

Read this

http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Date/Conversions.html

Sijo

use this

show_time = Time.now.strftime("%B %Y")

Regards,
Shyam
+91-971-618-9650
[email protected]

Check out strftime in the API.

shyam mohan wrote:

use this

show_time = Time.now.strftime("%B %Y")

Regards,
Shyam
+91-971-618-9650
[email protected]

Thank you very much. It works.

Ahmed A. 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::date::Conversions::DATE_FORMATS.merge!(:concise
=> “%d.%b.%y”)

Excerpt from:
http://ryandaigle.com/articles/2007/2/23/what-s-new-in-edge-rails-stop-littering-your-evnrionment-rb-with-custom-initializations

ActiveSupport::CoreExtensions::date::Conversions::DATE_FORMATS.merge!
(:concise
=> “%d.%b.%y”)

Excerpt from:
http://ryandaigle.com/articles/2007/2/23/what-s-new-in-edge-rails-stop-littering-your-evnrionment-rb-with-custom-initializations

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”

On Feb 18, 8:35 am, Ahmed A. [email protected] wrote:

shyam mohan wrote:

use this

show_time = Time.now.strftime(“%B %Y”)

Thank you very much. It works.

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

for what may be a more elegant solution. Certainly nicely centralized
if you want to use that format in more than one location.


Kurt W.
I am looking for a new Rails job:
http://www.CircleW.org/kurt/pages/resume

On Thu, Feb 18, 2010 at 8:06 AM, Sijo k g [email protected] wrote:

Hi Ahmed A.

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

[email protected] 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.

On Thu, Feb 18, 2010 at 7:57 AM, Ahmed A.
[email protected]wrote:

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

Any help?

@Conrad T. - 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