I want to convert 2 dates
"05/14/2020" to 05-14-2020 00:00:00. (start of the day)
"05/31/2020" to 05-31-2020 23:59:59. (end of the day)
I want to convert 2 dates
"05/14/2020" to 05-14-2020 00:00:00. (start of the day)
"05/31/2020" to 05-31-2020 23:59:59. (end of the day)
I would do this:
Date.strptime("05/14/2020", "%m/%d/%Y").beginning_of_day
#=> Thu, 14 May 2020 00:00:00 UTC +00:00
Date.strptime("05/31/2020", "%m/%d/%Y").end_of_day
#=> Sun, 31 May 2020 23:59:59 UTC +00:00
DateTime.strptime instead of DateDate#beginning_of_day is a Rails method (your question has the ruby-on-rails tag)ruby-on-rails and ruby. Therefore, we assume that your question is about Ruby on Rails. If your question isn't related to Rails, you should remove that tag (there's an edit button).You can use Time.strptime to parse a string that's composed of your date string and a fixed time value:
require 'time'
from = '05/14/2020'
to = '05/31/2020'
Time.strptime("#{from} 00:00:00", '%m/%d/%Y %H:%M:%S')
#=> 2020-05-14 00:00:00 +0200
Time.strptime("#{to} 23:59:59", '%m/%d/%Y %H:%M:%S')
#=> 2020-05-31 23:59:59 +0200
slice_before suggestion to my question perfectly worked for me. Since my question got downvoted, I deleted it. Thanks your solution resolved my problem. I will delete this comment once after you have seen it.