RahulOnRails
RahulOnRails

Reputation: 6542

ROR + Ruby Date From XML API

By using XML API, I got date-time as "2008-02-05T12: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 ?

Upvotes: 1

Views: 379

Answers (2)

the Tin Man
the Tin Man

Reputation: 160571

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.

Upvotes: 2

mu is too short
mu is too short

Reputation: 434765

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

Upvotes: 3

Related Questions