# How to convert a date in a model?

**URL:** <https://www.ruby-forum.com/t/how-to-convert-a-date-in-a-model/71953>\
**Category:** Rails\
**Created:** [October 12, 2006, 8:07pm UTC](https://www.ruby-forum.com/t/how-to-convert-a-date-in-a-model/71953 "2006-10-12T20:07:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [October 12, 2006, 8:07pm UTC](https://www.ruby-forum.com/t/how-to-convert-a-date-in-a-model/71953/1 "2006-10-12T20:07:30Z")

</div>

I have a table that has a date column price\_to\_start\_on.

I use a controller that gets data using the model, and renders to xml,  
the date comes out as yyyy\_mm\_dd format. I want the format to be  
yyyymmdd.

How do I do this, is it in the model? or the controller?

Ideally I would like it in the model, as it only has to be coded once.

Thanks

Andrew

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [October 13, 2006, 4:39am UTC](https://www.ruby-forum.com/t/how-to-convert-a-date-in-a-model/71953/2 "2006-10-13T04:39:46Z")

</div>

Andrew C. wrote:

> I have a table that has a date column price\_to\_start\_on.
> 
> I use a controller that gets data using the model, and renders to xml,  
> the date comes out as yyyy\_mm\_dd format. I want the format to be  
> yyyymmdd.
> 
> How do I do this, is it in the model? or the controller?

Use the Time#strftime method to convert a date/time to a string using a  
custom format. Something like  
rec.price\_to\_start\_on.strftime(’%Y%m%d’)  
should work for you. BTW, #strftime will cause an error if you call it  
on nil, so you might be better off checking for nil unless this column  
has a not-null constraint:  
rec.price\_to\_start\_on && rec.price\_to\_start\_on.strftime(’%Y%m%d’)

> Ideally I would like it in the model, as it only has to be coded once.

I’d put it in the view (if you have one), otherwise in the controller –  
since the format is related to the output, not to the underlying data  
(which is usually what the model is all about).

You can’t guarantee that you’re not going to want it in a different  
format later on for some other purpose. Putting it into the  
controller/view won’t lock you into a format for all future uses.

> Thanks
> 
> Andrew

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [October 18, 2006, 10:40am UTC](https://www.ruby-forum.com/t/how-to-convert-a-date-in-a-model/71953/3 "2006-10-18T10:40:17Z")

</div>

Uhm, correct me if i’m wrong, cos i’m all new to RoR + Ruby itself, but  
i think you could add a method to your usermodel that converts the  
date, like this:

class BlaBla \< ActiveRecord::Base  
…  
…  
def price\_to\_start\_on\_convdate  
self.price\_to\_start\_on.strftime("%Y%m%d")  
#or like this? gotta learn proper Ruby, damn 😃  
return self.price\_to\_start\_on.strftime("%Y%m%d")  
end

end

* * *

the in a view you could just use it like this:

\<%= someObject.price\_to\_start\_on\_convdate %\>

* * *

Hope that helps

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [October 19, 2006, 12:47pm UTC](https://www.ruby-forum.com/t/how-to-convert-a-date-in-a-model/71953/4 "2006-10-19T12:47:05Z")

</div>

I did add a method to the model as suggested. But I am not using views  
so have added the formatted date to the array of results. The data is  
then rendered as XML and is then available to my Flex application.

Thanks for the help.

Andrew

Thorsten L wrote:

> Uhm, correct me if i’m wrong, cos i’m all new to RoR + Ruby itself, but  
> i think you could add a method to your usermodel that converts the  
> date, like this:
> 
> class BlaBla \< ActiveRecord::Base  
> …  
> …  
> def price\_to\_start\_on\_convdate  
> self.price\_to\_start\_on.strftime("%Y%m%d")  
> #or like this? gotta learn proper Ruby, damn 😃  
> return self.price\_to\_start\_on.strftime("%Y%m%d")  
> end
> 
> end
> 
> * * *
> 
> the in a view you could just use it like this:
> 
> \<%= someObject.price\_to\_start\_on\_convdate %\>
> 
> * * *
> 
> Hope that helps

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [October 19, 2006, 2:15pm UTC](https://www.ruby-forum.com/t/how-to-convert-a-date-in-a-model/71953/5 "2006-10-19T14:15:38Z")

</div>

Is it correct to put that kind of thing in the model? I did that before  
with a date format - like Object#displaydate, and I was told that it was  
bad form - I should put it in a helper instead. Any thoughts on this?

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [October 19, 2006, 2:29pm UTC](https://www.ruby-forum.com/t/how-to-convert-a-date-in-a-model/71953/6 "2006-10-19T14:29:42Z")

</div>

i don’t really think there is a “wrong” or “right” for questions like  
this, it mainly is a matter of taste.

both solutions would be DRY in my opinion, the model-way adds a method  
to your object while a helper would be something like this:

\<%= display\_date(@yourobject.date) %\>
