views:

80

answers:

3
+2  Q: 

Date with ruby.

This is my date 20100816 and it integer date.

I want to display it like 08/16/2010.

What's the best way to do it?

+9  A: 

You can use the Date.strptime method to read a date in a given format and strftime to print it out in another format:

require 'date'
intdate = 20100816
Date.strptime(intdate.to_s, "%Y%m%d").strftime("%m/%d/%Y")
#=> "08/16/2010"
sepp2k
+1  A: 

You can use the Date.parse method and strftime to format the date

Date.parse(20100816.to_s).strftime("%m/%d/%Y")
Brent M
+1  A: 

Using the Date class is the most elegant solution. Another approach is to deal with it as a string:

d = 20100820
e = d.to_s
f = [e[4..5], e[6..7], e[0..3]].join('/')
glenn jackman