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?
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?
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"
You can use the Date.parse
method and strftime
to format the date
Date.parse(20100816.to_s).strftime("%m/%d/%Y")
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('/')