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

2019-01-26 04:17发布

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?

标签: ruby time
4条回答
ら.Afraid
2楼-- · 2019-01-26 04:34

The strftime method can be used to format times:

Time.now.strftime("%m/%d/%Y")
查看更多
小情绪 Triste *
3楼-- · 2019-01-26 04:42

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

查看更多
女痞
4楼-- · 2019-01-26 04:48

You can simply use %D with strftime method

 > Time.now.strftime("%D")
 => "11/24/14"  # mm/dd/yy
查看更多
爷、活的狠高调
5楼-- · 2019-01-26 04:50
my $current_time = DateTime->now();

my_current_time = DateTime.now

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

my_mdy = my_current_time.strftime("%m/%d/%Y")
查看更多
登录 后发表回答