krunal shah
krunal shah

Reputation: 16339

Display Date with ruby

This is my date 20100816 and it integer date.

How to display it like 08/16/2010?

Upvotes: 2

Views: 245

Answers (3)

glenn jackman
glenn jackman

Reputation: 246807

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('/')

Upvotes: 1

Brent M
Brent M

Reputation: 36

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

Date.parse(20100816.to_s).strftime("%m/%d/%Y")

Upvotes: 1

sepp2k
sepp2k

Reputation: 370162

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"

Upvotes: 9

Related Questions