Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I format a date to mm/dd/yyyy in Ruby?

Tags:

In Perl you can do:

my $current_time = DateTime->now(); my $mdy = $current_time->mdy("/"); 

What's the easiest way to do this in Ruby?

like image 870
Tom Lehman Avatar asked Aug 04 '09 22:08

Tom Lehman


People also ask

How do I change the Date format in Ruby?

Two steps: You need to convert your string into Date object. For that, use Date#strptime . You can use Date#strftime to convert the Date object into preferred format.

How do I change Date format to MDY?

Go to Format Cells > Custom Enter mm/dd/yyyy in the available space. Or you can use : Select a blank cell next to your date, for instance. I1, and type this formula =TEXT(G1, "yyyy-mm-dd"), and press Enter key, then drag AutoFill handle over the cells needed this formula.

What is Strftime in Ruby?

Ruby | Time strftime() function Time#strftime() is a Time class method which returns the time format according to the directives in the given format string. Syntax: Time.strftime() Parameter: Time values. Return: time format according to the directives in the given format string.


2 Answers

The strftime method can be used to format times:

Time.now.strftime("%m/%d/%Y") 
like image 101
sepp2k Avatar answered Oct 05 '22 23:10

sepp2k


I wrote a gem to help with formatting dates, and keeping your views DRY (not having to strftime every time you want to format dates).

Check it out at: http://github.com/platform45/easy_dates

like image 22
Ryan Avatar answered Oct 06 '22 00:10

Ryan