content01
content01

Reputation: 3225

Date format conversions Ruby

There's a simple way to convert a string like "28Feb11" to a ruby date object in this format: 28-02-2011 ?

Thank you!

Upvotes: 0

Views: 187

Answers (1)

Dave Newton
Dave Newton

Reputation: 160181

You can use strptime to create dates from arbitrary formats.

You can print out that date in your desired format with strftime.

> d = Date.strptime("28Feb11", "%d%b%y")
 => #<Date: 2011-02-28 (4911241/2,0,2299161)> 
> d.strftime("%d-%m-%Y")
 => "28-02-2011" 

Upvotes: 2

Related Questions