# Getting dates into m/d/yyyy format

**URL:** <https://www.ruby-forum.com/t/getting-dates-into-m-d-yyyy-format/69008>\
**Category:** Rails\
**Created:** [September 18, 2006, 5:09am UTC](https://www.ruby-forum.com/t/getting-dates-into-m-d-yyyy-format/69008 "2006-09-18T05:09:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shauna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@shauna](https://www.ruby-forum.com/u/shauna)\
**Post date:** [September 18, 2006, 5:09am UTC](https://www.ruby-forum.com/t/getting-dates-into-m-d-yyyy-format/69008/1 "2006-09-18T05:09:45Z")

</div>

I’ve tried the simple\_date\_select plugin and it works as advertised  
(Thank you so much!), that is it converts the unwieldy default date  
selection boxes into a single text field with the default date format  
like yyyy-mm-dd. And, I played with the extension to that plugin  
proposed by Jeremy E. that uses strftime(’%m/%d/%Y’) to get it  
reformatted like mm/dd/yyyy (Thank you, too, Jeremy).

My problem is that I can’t quite get strftime to format the dates  
exactly as I’d like to see them, which is like 6/3/2006 (no zero or  
space before the 6 and no zero or space before the 3). Being new to  
Ruby, I’m weak on Ruby syntax. Jeremy’s suggested code is:  
def to\_date\_select\_tag(options = {})  
to\_input\_field\_tag(‘text’, {‘size’=\>‘10’,  
‘value’=\>value.strftime(’%m/%d/%Y’)}.merge(options))  
end

I tried to substitute a block {} for the expression  
value.strftime(’%m/%d/%Y’) and failed miserably. Can anyone help me out  
with some expression to convert a date string like 06/03/2006 to one  
like 6/3/2006 that I can plug into the above method?

Thanks, this forum has been SOOOO helpful!

Shauna

---

<div class="post-metadata">

**Author:** ![shauna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@shauna](https://www.ruby-forum.com/u/shauna)\
**Post date:** [September 18, 2006, 5:48am UTC](https://www.ruby-forum.com/t/getting-dates-into-m-d-yyyy-format/69008/2 "2006-09-18T05:48:59Z")

</div>

How about

date = Date.today  
“#{date.month}/#{date.day}/#{date.year}”

Long

---

<div class="post-metadata">

**Author:** ![shauna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@shauna](https://www.ruby-forum.com/u/shauna)\
**Post date:** [September 19, 2006, 10:22pm UTC](https://www.ruby-forum.com/t/getting-dates-into-m-d-yyyy-format/69008/3 "2006-09-19T22:22:08Z")

</div>

There’s one more item to consider here, and that’s how to treat nil  
dates. I did figure out the answer to this one, so I’m posting the  
solution here for others who might want to alter the plugin similarly to  
get a m/d/yyyy format:

Replace the plugin line  
to\_input\_field\_tag(‘text’, {‘size’=\>‘10’}.merge(options))

with  
if value.nil? then  
to\_input\_field\_tag(‘text’, {‘size’=\>‘10’}.merge(options))  
else  
to\_input\_field\_tag(‘text’, {‘size’=\>‘10’,  
‘value’=\>"#{value.month}/#{value.day}/#{value.year}"}.merge(options))  
end

I hope this helps others. I haven’t tried altering the datetime portion  
of the plugin because I don’t need datetimes, but I imagine similar code  
would be needed there as well.

Shauna

---

<div class="post-metadata">

**Author:** ![shauna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@shauna](https://www.ruby-forum.com/u/shauna)\
**Post date:** [September 18, 2006, 6:34am UTC](https://www.ruby-forum.com/t/getting-dates-into-m-d-yyyy-format/69008/4 "2006-09-18T06:34:44Z")

</div>

Wow, Long, that was perfect!

Shauna
