Francisco
Francisco

Reputation: 1557

Parse Date From an Array

I am consuming a Rest API and the return of one of the data is a datetime that is return in an Array. Exactly like this:

"beginDate": [2018, 7, 24, 8, 22, 0, 0]

In my rails app I want to parse this data into a date time format. I already try this way:

Date.new(object["beginDate"].join)

And this way:

Date.parse(object["beginDate"].join)

Also this:

DateTime.new(object["beginDate"].join)
DateTime.parse(object["beginDate"].join)

Every code return an error like: 'Invalid date'.

How can I parse the data in to a datetime format?

Upvotes: 0

Views: 107

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121010

This is a DateTime object, not a Date in the first place. DateTime#new accepts seven arguments, hence all you need is to splat an array you have:

DateTime.new(*object["beginDate"])

Upvotes: 3

Related Questions