# How to use at\_beginning\_of\_year

**URL:** <https://www.ruby-forum.com/t/how-to-use-at-beginning-of-year/63330>\
**Category:** Rails\
**Created:** [June 14, 2006, 9:34am UTC](https://www.ruby-forum.com/t/how-to-use-at-beginning-of-year/63330 "2006-06-14T09:34:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [June 14, 2006, 9:34am UTC](https://www.ruby-forum.com/t/how-to-use-at-beginning-of-year/63330/1 "2006-06-14T09:34:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [June 14, 2006, 9:42am UTC](https://www.ruby-forum.com/t/how-to-use-at-beginning-of-year/63330/2 "2006-06-14T09:42:24Z")

</div>

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 😉

Just try this instead : Time.now.at\_beginning\_of\_year  
It should work better

Cheers,

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [June 14, 2006, 9:56am UTC](https://www.ruby-forum.com/t/how-to-use-at-beginning-of-year/63330/3 "2006-06-14T09:56:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [June 14, 2006, 10:36am UTC](https://www.ruby-forum.com/t/how-to-use-at-beginning-of-year/63330/4 "2006-06-14T10:36:59Z")

</div>

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