# Weird dates problem

**URL:** <https://www.ruby-forum.com/t/weird-dates-problem/186172>\
**Category:** Rails\
**Created:** [April 1, 2010, 4:40pm UTC](https://www.ruby-forum.com/t/weird-dates-problem/186172 "2010-04-01T16:40:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [April 1, 2010, 4:40pm UTC](https://www.ruby-forum.com/t/weird-dates-problem/186172/1 "2010-04-01T16:40:13Z")

</div>

HI,  
I have a pretty weird problems with dates.  
my objective was to parse some text files and put them into a  
database.

the lines of the file are divided with a | and some of the fields are  
dates in this format dd.mm.yyyy  
now in the parsing a get the system date to skip parsing elements in  
the past  
@date is the local date created with @date = Time.now.to\_date  
params[:to] is the string from the file with the date in above format  
I did this with a simple

if params && ( params[:to].to\_date \> @date ) # nie parsujemy starych  
cennikow  
Price.create\_or\_update params  
end

on my local enviroment everything works fine but on the remote host i  
get an invalid date ArgumentError

ruby version on host  
ruby 1.8.6 (2007-06-07 patchlevel 36) [i386-linux]  
local  
ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-linux]

any ideas?  
thanks in advance

---

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [April 1, 2010, 4:51pm UTC](https://www.ruby-forum.com/t/weird-dates-problem/186172/2 "2010-04-01T16:51:55Z")

</div>

On 1 April 2010 15:36, Adam [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> params[:to] is the string from the file with the date in above format  
> I did this with a simple
> 
> if params && ( params[:to].to\_date \> @date ) # nie parsujemy starych  
> cennikow  
> Price.create\_or\_update params  
> end
> 
> on my local enviroment everything works fine but on the remote host i  
> get an invalid date ArgumentError

Catch the error in your code and log or display the params[:to] that  
is causing it. Then you will likely see the problem. It is risky to  
convert strings to date without checking validity and taking  
appropriate action anyway.

Colin

---

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [April 7, 2010, 12:42pm UTC](https://www.ruby-forum.com/t/weird-dates-problem/186172/3 "2010-04-07T12:42:48Z")

</div>

sory for the long absence (holidays)

well the params[:to] is a date in this format 27.02.2010, but when i  
try to log params[:to].to\_date I get an error  
NoMethodError( undefined method’[]’ for nil:NilClass)

---

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [April 7, 2010, 1:55pm UTC](https://www.ruby-forum.com/t/weird-dates-problem/186172/4 "2010-04-07T13:55:58Z")

</div>

I’ve tried that too but stille get an argument error with invalid  
date, i’ve log the dates an they look fine (the format’s ok) ;/  
Really hate this kind of errors

---

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [April 7, 2010, 12:51pm UTC](https://www.ruby-forum.com/t/weird-dates-problem/186172/5 "2010-04-07T12:51:35Z")

</div>

Le 07/04/2010 12:41, Adam a écrit :

> > > the lines of the file are divided with a | and some of the fields are  
> > > Price.create\_or\_update params
> > 
> > > ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-linux]  
> > > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.

Hi,

Assuming params[:to] is a string like “31.12.2007”  
you should try :

Date.parse(params[:to])

–  
Aurélien AMILIN

---

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [April 7, 2010, 2:15pm UTC](https://www.ruby-forum.com/t/weird-dates-problem/186172/6 "2010-04-07T14:15:18Z")

</div>

I’ll jutr try (for now) to make the dates by spliting the string and  
extracting every part of it and making a new Date obj. from those  
parts

---

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [April 7, 2010, 3:58pm UTC](https://www.ruby-forum.com/t/weird-dates-problem/186172/7 "2010-04-07T15:58:18Z")

</div>

With strptime you can specify the format:  
Date.strptime(“07.04.2010”, “%d.%m.%Y”)
