How to convert timestamp with ruby

前端 未结 1 1969
再見小時候
再見小時候 2021-01-27 20:06

Is it possible to convert time with ruby? My program prints the time stamp like this: 10/18/2107 14:08:09, but I want it to be like this: 2017 M10 18, Wed 14:

相关标签:
1条回答
  • 2021-01-27 20:57

    From the string you can parse it to DateTime passing the format, and then format the output string:

    date_time = '10/18/2107 14:08:09'
    
    puts DateTime.strptime(date_time, '%m/%d/%Y %H:%M:%S')
                 .strftime('%Y M%m %d, %a %H:%M:%S %Z')
    

    Note it's Tuesday and the time zone it's specified as 00:00.

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