convert string to specific datetime format?

前端 未结 7 830
孤城傲影
孤城傲影 2020-12-24 11:21

String

\"2011-05-19 10:30:14\"

To

Thu May 19 10:30:14 UTC 2011

How can I convert specific string to this

7条回答
  •  萌比男神i
    2020-12-24 12:14

    No need to apply anything. Just add this code at the end of variable to which date is assigned. For example

       @todaydate = "2011-05-19 10:30:14"
       @todaytime.to_time.strftime('%a %b %d %H:%M:%S %Z %Y')
    

    You will get proper format as you like. You can check this at Rails Console

    Loading development environment (Rails 3.0.4)
    ruby-1.9.2-p136 :001 > todaytime = "2011-05-19 10:30:14"
     => "2011-05-19 10:30:14" 
    ruby-1.9.2-p136 :002 > todaytime
     => "2011-05-19 10:30:14" 
    ruby-1.9.2-p136 :003 > todaytime.to_time
     => 2011-05-19 10:30:14 UTC
    ruby-1.9.2-p136 :008 > todaytime.to_time.strftime('%a %b %d %H:%M:%S %Z %Y')
     => "Thu May 19 10:30:14 UTC 2011"
    

    Try 'date_format' gem to show date in different format.

提交回复
热议问题