1

Using an API, i can retrieve an orders list from a website.

in this list, i have for each order, the date of presentation.

i need to filter this list for order placed in a specific range, but using the date range i have "bad value for range"

this is how the API present the date:

"date_created"=>"2017-03-08T11:22:00"

This is my code

@lunedi = Date.today.at_beginning_of_week
@martedi = Date.today.at_beginning_of_week + 1
ele_ordini = woocommerce.get("orders").parsed_response
@ele_ordini = ele_ordini.select { |ordine| ordine['date_created'] == lunedi.to_datetime..martedi.to_datetime }

My goal si to filter for order placed in current week, only on Monday and Tuesday

yes, the code must be changed ot fill all Tuesday day until 23:59!! but i can do it after!

3 Answers 3

2

You trying to use active record notation which doesn't work with the select method. Something like this should work.

@ele_ordini = ele_ordini.select do |ordine| 
  tmp = ordine['date_created'].to_datetime
  tmp >= lunedi.to_datetime && tmp <= martedi.to_datetime
end
Sign up to request clarification or add additional context in comments.

1 Comment

Great! Thanks ;-)
0

If you just care about comparing the dates (and you're using Rails - which it looks like you are), then you could simplify this somewhat. For example:

@lunedi = Date.today.at_beginning_of_week
ele_ordini = woocommerce.get("orders").parsed_response
@ele_ordini = ele_ordini.select { |ordine| ordine['date_created'].to_date - @lunedi < 2 }

i.e. you're only looking at the difference in days between each date and Monday, and selecting anything that is 0 (Monday) or 1 (Tuesday)

Comments

0

You need to state the unit of time when adding it to a variable. Try this

@lunedi = Date.today.at_beginning_of_week
@martedi = Date.today.at_beginning_of_week + 1.day
ele_ordini = woocommerce.get("orders").parsed_response
@ele_ordini = ele_ordini.select { |ordine| (lunedi..martedi).include? ordine['date_created'] }

Comments

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.