By using XML API, I got date-time as "2008-02-05开发者_开发技巧T12:50:00Z"
. Now I wanna convert this text format into different format like "2008-02-05 12:50:00"
. But I am getting proper way.
I have tried this one :: @a = "2008-02-05T12:50:00Z"
Steps
1. @a.to_date
=> Tue, 05 Feb 2008
2. @a.to_date.strftime('%Y')
=> "2008"
3. @a.to_date.strftime('%Y-%m-%d %H:%M:%S')
=> "2008-02-05 00:00:00
Suggest some thing ?
The to_date
method converts your string to a date but dates don't have hours, minutes, or seconds. You want to use DateTime
:
require 'date'
d = DateTime.parse('2008-02-05T12:50:00Z')
d.strftime('%Y-%m-%d %H:%M:%S')
# 2008-02-05 12:50:00
Use Ruby's DateTime:
DateTime.parse("2008-02-05T12:50:00Z") #=> #<DateTime: 2008-02-05T12:50:00+00:00 (353448293/144,0/1,2299161)>
From there you can output the value in any format you want using strftime
. See Time#strftime
for more info.
精彩评论