如何在Ruby中将日期格式化为mm / dd / yyyy?

在Perl中你可以做到:

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

在Ruby中最简单的方法是什么?

strftime方法可用于格式化时间:

 Time.now.strftime("%m/%d/%Y") 

我写了一个gem来帮助格式化日期,并保持你的视图DRY(每次你想格式化日期时都不需要strftime)。

请查看: http : //github.com/platform45/easy_dates

你可以简单地使用%Dstrftime方法

  > Time.now.strftime("%D") => "11/24/14" # mm/dd/yy 
 my $current_time = DateTime->now(); my_current_time = DateTime.now my $mdy = $current_time->mdy("/"); my_mdy = my_current_time.strftime("%m/%d/%Y")