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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
s = "06/07/2008" d = Date.parse(s) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Greg Hauptmann
2008-Jun-08 01:50 UTC
Re: how to parse a "dd/mm/yyyy" formatted date string???
funny, this isn''t working for me... On 6/8/08, alberto <buildoutmanager-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > s = "06/07/2008" > > d = Date.parse(s) > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Greg Hauptmann
2008-Jun-08 03:38 UTC
Re: how to parse a "dd/mm/yyyy" formatted date string???
I get an ArgumentError: Invalid Date? I''m on: Ruby 1.8.6 Rails 2.0.2 Any ideas? On 6/8/08, Greg Hauptmann <greg.hauptmann.ruby-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> funny, this isn''t working for me... > > On 6/8/08, alberto <buildoutmanager-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> s = "06/07/2008" >> >> d = Date.parse(s) >> >> >> >> >> >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Greg Hauptmann
2008-Jun-08 06:39 UTC
Re: how to parse a "dd/mm/yyyy" formatted date string???
I found on the Ruby forum the following: Date.strptime(''28/03/2008'', ''%d/%m/%Y'') On Sun, Jun 8, 2008 at 3:37 PM, Phlip <phlip2005-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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. > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Daniel Waite
2008-Jun-08 09:21 UTC
Re: how to parse a "dd/mm/yyyy" formatted date string???
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Michael Breen
2008-Jun-08 12:18 UTC
Re: how to parse a "dd/mm/yyyy" formatted date string???
#to_date or #to_time will throw an ArgumentError for the format Greg is working with, dd/mm/yyyy, best. mike On Jun 8, 2008, at 5:21 AM, Daniel Waite wrote:> > 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. > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
>> 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? (-: --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Jun-08 15:01 UTC
Re: how to parse a "dd/mm/yyyy" formatted date string???
If the date helpers aren''t being kind: if date_string =~ %r{(\d+)/(\d+)/(\d+)} Date::civil($3.to_i, $2.to_i, $1.to_i) end Fred --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Daniel Waite
2008-Jun-08 21:09 UTC
Re: how to parse a "dd/mm/yyyy" formatted date string???
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? (-:http://mad.ly/2008/04/09/rails-21-time-zone-support-an-overview/ Difficult things should be possible. :) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Daniel Waite
2008-Jun-08 21:10 UTC
Re: how to parse a "dd/mm/yyyy" formatted date string???
Michael Breen wrote:> #to_date or #to_time will throw an ArgumentError for the format Greg > is working with, dd/mm/yyyy, > > best. > mikeDid you run the code in console? Try it. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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! On Jun 8, 2:10 pm, Daniel Waite <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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. > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Michael Breen
2008-Jun-09 11:46 UTC
Re: how to parse a "dd/mm/yyyy" formatted date string???
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 On Jun 8, 2008, at 5:10 PM, Daniel Waite wrote:> > 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. > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
KathysKode-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Jun-09 15:21 UTC
Re: how to parse a "dd/mm/yyyy" formatted date string???
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 On Jun 8, 12:39 am, "Greg Hauptmann" <greg.hauptmann.r...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I found on the Ruby forum the following: > Date.strptime(''28/03/2008'', ''%d/%m/%Y'') > > > > On Sun, Jun 8, 2008 at 3:37 PM, Phlip <phlip2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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.- Hide quoted text - > > - Show quoted text ---~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Jun-09 15:26 UTC
Re: how to parse a "dd/mm/yyyy" formatted date string???
On 9 Jun 2008, at 16:21, KathysKode-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> > 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> > 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 > On Jun 8, 12:39 am, "Greg Hauptmann" <greg.hauptmann.r...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: >> I found on the Ruby forum the following: >> Date.strptime(''28/03/2008'', ''%d/%m/%Y'') >> >> >> >> On Sun, Jun 8, 2008 at 3:37 PM, Phlip <phlip2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >>> 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.- Hide quoted text - >> >> - Show quoted text - > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Greg Hauptmann
2008-Jun-09 21:34 UTC
Re: how to parse a "dd/mm/yyyy" formatted date string???
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 On Tue, Jun 10, 2008 at 1:26 AM, Frederick Cheung < frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On 9 Jun 2008, at 16:21, KathysKode-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: > > > > > 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 > > > > > > 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 > > On Jun 8, 12:39 am, "Greg Hauptmann" <greg.hauptmann.r...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > >> I found on the Ruby forum the following: > >> Date.strptime(''28/03/2008'', ''%d/%m/%Y'') > >> > >> > >> > >> On Sun, Jun 8, 2008 at 3:37 PM, Phlip <phlip2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> > >>> 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.- Hide quoted text - > >> > >> - Show quoted text - > > > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Daniel Waite
2008-Jun-11 00:34 UTC
Re: how to parse a "dd/mm/yyyy" formatted date string???
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On Jun 11, 5:34 am, Daniel Waite <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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. > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Michael Breen
2008-Jun-25 11:18 UTC
Re: how to parse a "dd/mm/yyyy" formatted date string???
On Jun 25, 2008, at 5:29 AM, Titan wrote:> > 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_sDaniel''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> > > 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 > > On Jun 11, 5:34 am, Daniel Waite <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: >> 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. >> -- >> Posted viahttp://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks, Mike. the to_time did work for me :). However to_date still keeps throwing out the same error. Just in case this is version specific, I''m using rails 1.1.6 and ruby 1.8.4. ~Titan On Jun 25, 4:18 pm, Michael Breen <hard...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jun 25, 2008, at 5:29 AM, Titan wrote: > > > > > 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 > > > > > 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 > > > On Jun 11, 5:34 am, Daniel Waite <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > wrote: > >> 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. > >> -- > >> Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---