How can I use US-style dates in Rails using Ruby 1.9?

后端 未结 7 1835
攒了一身酷
攒了一身酷 2021-01-04 09:21

I\'m in the U.S., and we usually format dates as "month/day/year". I\'m trying to make sure that my Rails app, using Ruby 1.9, assumes this format everywhere, and

7条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-04 09:56

    For parsing US-style dates, you could use:

    Date.strptime(date_string, '%m/%d/%Y')
    

    In console:

    > Date.strptime('04/01/2011', '%m/%d/%Y')
     => Fri, 01 Apr 2011 
    > Date.strptime('4/1/2011', '%m/%d/%Y')
     => Fri, 01 Apr 2011 
    

提交回复
热议问题