0

I know it is common error but I could not resolve it even after trying those answers.

Through the rest api I am sending some parameters inculdes dates. I am recieving all the data in the method where I want but when I am trying to parse Date it throws error of invalid date.

Here are my parameters that I am recieving

 {"uid"=>"1", "user"=>"abc.a@abc.com", "from"=>"Mon Nov 3 24:59:12 CET 2014", "to"=>"Tue Nov 11 24:59:12 CET 2014"}

and Date format is

Mon Nov 3 24:59:12 CET 2014

but it is throwing error on parsing on line below

 fr = DateTime.parse(params[:from]) unless params[:from].empty?

I tried strptime as well but did not work.

Imp points is I need hour also for later processing. Thanks

Caffeine Coder
  • 1,869
  • 1
  • 17
  • 35
user2323
  • 49
  • 2
  • 11

2 Answers2

0

what you are doing wrong is parsing DateTime while it is just date and should be parsed as one of the following ways:

1.

>> fr = params[:from].to_date unless params[:from].empty?
=>  Mon, 03 Nov 2014

2.

>> fr = Date.parse(params[:from]) unless params[:from].empty?
=> Mon, 03 Nov 2014
Abdul Baig
  • 3,683
  • 3
  • 21
  • 48
0

You have 24:59 which is invalid time. Anyway, use strptime:

DateTime.strptime("Mon Nov 3 22:59:12 CET 2014", "%a %b %e %T %Z %Y")
#=> Mon, 03 Nov 2014 22:59:12 +0100
Felix Borzik
  • 1,230
  • 12
  • 19