How to use at_beginning_of_year


#1

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 %>


#2

Le Mercredi 14 Juin 2006 09:34, Glenn C. 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 :wink:

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

Cheers,


#3

Loïc Guitaut wrote:

Le Mercredi 14 Juin 2006 09:34, Glenn C. 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 :wink:

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 :slight_smile: )

YYYY-MM-DD
eg.
2007.01.01


#4

Le Mercredi 14 Juin 2006 09:56, Glenn C. a écrit :

This is almost good :wink:

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 :slight_smile: )

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 :slight_smile:

So with your example it should be :
Time.now.at_beginning_of_year.to_s :my_beautiful_date