Hi,
I'm stuck. How do you parse a "dd/mm/yyyy" formatted date string???
I get a date format error.
Thanks in advance Greg
Hi,
I'm stuck. How do you parse a "dd/mm/yyyy" formatted date string???
I get a date format error.
Thanks in advance Greg
s = "06/07/2008"
d = Date.parse(s)
funny, this isn't working for me...
I get an ArgumentError: Invalid Date?
I'm on: Ruby 1.8.6 Rails 2.0.2
Any ideas?
It's supposed to be strptime, to match Time.strftime to _output_ a date in a given format. Googling for that seems to yield either people using strptime or complaining about it.
I found on the Ruby forum the following:
Date.strptime(‘28/03/2008’, ‘%d/%m/%Y’)
Greg Hauptmann wrote:
Hi,
I'm stuck. How do you parse a "dd/mm/yyyy" formatted date string???
'12/14/1981'.to_date
=> Mon, 14 Dec 1981
'12/14/1981'.to_time
=> Mon Dec 14 00:00:00 UTC 1981
Easy things should be easy.
#to_date or #to_time will throw an ArgumentError for the format Greg
is working with, dd/mm/yyyy,
best. mike
I'm stuck. How do you parse a "dd/mm/yyyy" formatted date string???
'12/14/1981'.to_date
=> Mon, 14 Dec 1981
'12/14/1981'.to_time
=> Mon Dec 14 00:00:00 UTC 1981
Easy things should be easy.
In what locale? (-:
If the date helpers aren't being kind: if date_string =~ %r{(\d+)/(\d+)/(\d+)} end
Fred
Phlip wrote:
I'm stuck. How do you parse a "dd/mm/yyyy" formatted date string???
'12/14/1981'.to_date
=> Mon, 14 Dec 1981
'12/14/1981'.to_time
=> Mon Dec 14 00:00:00 UTC 1981
Easy things should be easy.
In what locale? (-:
Difficult things should be possible.
Michael Breen wrote:
#to_date or #to_time will throw an ArgumentError for the format Greg is working with, dd/mm/yyyy,
best. mike
Did you run the code in console? Try it.
That's really odd... you might want to try doing a Time.parse(s) also...
Thanks for the tip about #to_date and #to_time. You gotta love Rails ActiveSupport!
yeah, I tried in the console using the format that Greg is working
with (dd/mm/yyyy). Your example works fine (mm/dd/yyyy).
>> '14/12/1981'.to_date
ArgumentError: invalid date
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/
ruby/1.8/date.rb:727:in `new'
from /Library/Ruby/Gems/1.8/gems/activesupport-2.0.2/lib/
active_support/core_ext/string/conversions.rb:19:in `to_date'
from (irb):1
>> '14/12/1981'.to_time
ArgumentError: invalid date
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/
ruby/1.8/date.rb:1482:in `civil'
from /Library/Ruby/Gems/1.8/gems/activesupport-2.0.2/lib/
active_support/core_ext/time/calculations.rb:39:in
`time_with_datetime_fallback'
from /Library/Ruby/Gems/1.8/gems/activesupport-2.0.2/lib/
active_support/core_ext/time/calculations.rb:44:in `utc_time'
from /Library/Ruby/Gems/1.8/gems/activesupport-2.0.2/lib/
active_support/core_ext/string/conversions.rb:15:in `send'
from /Library/Ruby/Gems/1.8/gems/activesupport-2.0.2/lib/
active_support/core_ext/string/conversions.rb:15:in `to_time'
from (irb):2
Greg, This is the hard part of following this forum...where is the oyster that has the pearl? I see you excellent bit of information in; Date.strptime('28/03/2008', '%d/%m/%Y') Obviously, this code is in your controller. It the field on the left of the parens supposed to be the params that was passed from the form. Dates are so screwy in Ruby. I've never been able to figure out or see anyone take a date from a user in a form_tag (non model input) and process it into the controller. I've recently take the ridiculous 'long way around the barn' and instaniated a DATE field object in a placebo table to accept the value; example; y = Badboy.new y.playdate = row[1] if @asset.mtxes.find(:first, :conditions => {:mdate => y.playdate }) @asset.mtxes.find(:first, :conditions => {:mdate => y.playdate }).destroy end Badboy is a table that's simply being instantiated for the brief time that I need to borrow a WORKING datefield object. Pretty crazy, huh? Kathleen
Greg, This is the hard part of following this forum...where is the oyster that has the pearl? I see you excellent bit of information in; Date.strptime('28/03/2008', '%d/%m/%Y') Obviously, this code is in your controller. It the field on the left of the parens supposed to be the params that was passed from the form. Dates are so screwy in Ruby. I've never been able to figure out or see anyone take a date from a user in a form_tag (non model input) and process it into the controller.
if you're doing that then you don't need to be messing around with
parsing strings. As part of the params you get a hash, so (by default)
the respective components of the date are params[:date][:year],
params[:date][:month] and params[:date][:day]. Just call to_i on these
and pass them to Time.mktime or Date::civil
Fred
thanks guys but, the issue in my use case is that I’m actually uploading a CSV file and then parsing it - so it’s actually the CSV parsing routines that I’m using directly. So my questions was more around Ruby data handling I guess than Rails (as in my use case I’m not really using Rails directly)
Cheers Greg
Michael Breen wrote:
yeah, I tried in the console using the format that Greg is working with (dd/mm/yyyy). Your example works fine (mm/dd/yyyy).
Oi. Yay me for not paying closer attention. You're right, mm/dd/yyyy does not get parsed by #to_date or #to_time. My apologies.
To the OT, because you know the format is mm/dd, flip them around first?
If I could get to my array#fold method we could do it in one, short line, but alas... my site is down. So temporary variable here we go...
components = '14/12/1981'.split('/') good_date = components[1] + '/' + # you get the idea... good_date.to_date
I'm sure someone else has a nicer solution.
Hello,
I have been facing the same problem as Greg, but unfortunately neither
of these worked for me
dateStr = params[:startdate].to_s logger.info "date - " + dateStr startdate =Date.strptime(dateStr, '%m/%d/%Y').to_s
gives me -
date - 05/16/2008 ArgumentError: invalid date
Quite surprisingly, the dateStr being printed seems to be quite correct yet the invalid date exception is thrown !! The same thing happens with the Date.parse too.. I have been trying to resolve this for quite some time now and any help in this would be great !
~Titan
Hello,
I have been facing the same problem as Greg, but unfortunately neither of these worked for me
dateStr = params[:startdate].to_s logger.info "date - " + dateStr startdate =Date.strptime(dateStr, '%m/%d/%Y').to_s
Daniel's tip to convert a string to a date should work for you:
startdate = dateStr.to_date => Fri, 16 May 2008
or
startdate = dateStr.to_time => Fri May 16 00:00:00 UTC 2008
Best. Mike