Forum: Ruby on Rails How to use at_beginning_of_year

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
880f96b24ef64881d9ee2e9f062d59a8?d=identicon&s=25 Glenn C. (glenn)
on 2006-06-14 09:34
I want to get the Date in formation YYYY-MM-DD for the start of the
current year

But I cannot think how to do it...

This generates a name error .rhtml view.

 <%= at_beginning_of_year.Time.now  %>
Loïc Guitaut (Guest)
on 2006-06-14 09:42
(Received via mailing list)
Le Mercredi 14 Juin 2006 09:34, Glenn Cadman a écrit :
> I want to get the Date in formation YYYY-MM-DD for the start of the
> current year
>
> But I cannot think how to do it...
>
> This generates a name error .rhtml view.
>
>  <%= at_beginning_of_year.Time.now  %>

This is almost good ;)

Just try this instead : Time.now.at_beginning_of_year
It should work better

Cheers,
880f96b24ef64881d9ee2e9f062d59a8?d=identicon&s=25 Glenn C. (glenn)
on 2006-06-14 09:56
Loïc Guitaut wrote:
> Le Mercredi 14 Juin 2006 09:34, Glenn Cadman a écrit :
>> I want to get the Date in formation YYYY-MM-DD for the start of the
>> current year
>>
>> But I cannot think how to do it...
>>
>> This generates a name error .rhtml view.
>>
>>  <%= at_beginning_of_year.Time.now  %>
>
> This is almost good ;)
>
> Just try this instead : Time.now.at_beginning_of_year
> It should work better
>
> Cheers,

Yes it works, but how do I now format the date to my desired format
(dates the way god intented :) )

YYYY-MM-DD
eg.
2007.01.01
Loïc Guitaut (Guest)
on 2006-06-14 10:36
(Received via mailing list)
Le Mercredi 14 Juin 2006 09:56, Glenn Cadman a écrit :
> >
> > This is almost good ;)
> >
> > Just try this instead : Time.now.at_beginning_of_year
> > It should work better
> >
> > Cheers,
>
> Yes it works, but how do I now format the date to my desired format
> (dates the way god intented :) )
>
Ok, you'll have to write something like this at the end of your
config/environment.rb :

ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(
	:my_beautiful_date => "%Y-%m-%d"
)

And now on any Time object you can call the to_s method with
the :my_beautiful_date parameter and it should work perfectly :)

So with your example it should be :
Time.now.at_beginning_of_year.to_s :my_beautiful_date
This topic is locked and can not be replied to.