Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert ruby date to string without losing format

I'm trying to convert a Ruby Date object to a string. The format of the date is: Sun, 15 Sep 2013

However, when I convert it to a string using #to_s it gives me the following: "2013-09-15"

Instead, I want it to become: "Sun, 15 Sep 2013"

like image 620
bytebiscuit Avatar asked Jan 30 '14 22:01

bytebiscuit


1 Answers

Use Date#strftime there are so many options

require 'date'


date = Date.parse("Sun, 15 Sep 2013") # => #<Date: 2013-09-15 ((2456551j,0s,0n),+0s,2299161j)>

date.strftime("%a, %d %b %Y") # => "Sun, 15 Sep 2013"
like image 200
bjhaid Avatar answered Oct 23 '22 05:10

bjhaid