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

前端 未结 4 1197
遇见更好的自我
遇见更好的自我 2020-12-18 18:56

In Perl you can do:

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

What\'s the easiest way to do this in Ru

相关标签:
4条回答
  • 2020-12-18 19:00

    The strftime method can be used to format times:

    Time.now.strftime("%m/%d/%Y")
    
    0 讨论(0)
  • 2020-12-18 19:02

    You can simply use %D with strftime method

     > Time.now.strftime("%D")
     => "11/24/14"  # mm/dd/yy
    
    0 讨论(0)
  • 2020-12-18 19:12
    my $current_time = DateTime->now();
    
    my_current_time = DateTime.now
    
    my $mdy = $current_time->mdy("/");
    
    my_mdy = my_current_time.strftime("%m/%d/%Y")
    
    0 讨论(0)
  • 2020-12-18 19:22

    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

    0 讨论(0)
提交回复
热议问题