0

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?

1 Answer 1

3

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"])
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, for trying to help. I already had try with the DateTime.new, with out the *, as a put above and didn't work. With the * before it work, what that symbol does?
Here it allows the array to be passed as the list of parameters. Here is a good writing or just google “ruby splat operator.”
Thanks, It helps me alot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.