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? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi Ahmed Abdelsalam Read this http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Date/Conversions.html Sijo -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
use this
show_time = Time.now.strftime("%B %Y")
Regards,
Shyam
+91-971-618-9650
shyam-/SZh5OcqMinR7s880joybQ@public.gmane.org
On Thu, Feb 18, 2010 at 9:27 PM, Ahmed Abdelsalam
<lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>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.
>
> Any help?
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To unsubscribe from this group, send email to
>
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
> .
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Check out strftime in the API. On Thu, Feb 18, 2010 at 5:06 PM, Sijo k g <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi Ahmed Abdelsalam > > Read this > > http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Date/Conversions.html > > > Sijo > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
shyam mohan wrote:> use this > > show_time = Time.now.strftime("%B %Y") > > Regards, > Shyam > +91-971-618-9650 > shyam-/SZh5OcqMinR7s880joybQ@public.gmane.orgThank you very much. It works. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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::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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> 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::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-initializationsYou 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" -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Feb 18, 8:35 am, Ahmed Abdelsalam <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 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. -- Kurt Werle I am looking for a new Rails job: http://www.CircleW.org/kurt/pages/resume -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
kwerle-e+AXbWqSrlAAvxtiuMwx3w@public.gmane.org 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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Thu, Feb 18, 2010 at 8:06 AM, Sijo k g <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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> -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Thu, Feb 18, 2010 at 7:57 AM, Ahmed Abdelsalam <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>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?> -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
@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
On 19 Lut, 05:13, Conrad Taylor
<conra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> On Thu, Feb 18, 2010 at 7:57 AM, Ahmed Abdelsalam
<li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>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?
>
> > --
> > Posted viahttp://www.ruby-forum.com/.
>
> > --
> > You received this message because you are subscribed to the Google
Groups
> > "Ruby on Rails: Talk" group.
> > To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> > To unsubscribe from this group, send email to
> >
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/rubyonrails-talk?hl=en.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.