I am getting the month and day reversed in parsing a date. I am wondering if there is some intelligence in Ruby that knows that at the moment I am in Mexico, as the parsing is working as if I was "6/15/2008".to_date => ArgumentError: invalid date "15/6/2008".to_date => Sun, 15 Jun 2008 What I really want is my date to be understood as month/day/year. Is there a way to tell Ruby or Rails to do this? Been poking around on google and do not see an answer that works here. I found a page with this example, which makes me think maybe there is something weird going on due to my geographic location: ''06/15/2008''.to_date <http://apidock.com/rails/ActiveSupport/CoreExtensions/String/Conversions/to_date> # => Sun, 15 Jun 2008 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Ok, feel a little stupid, if I put the date in the form yyyy-mm-dd and then parse it I get the right result. Anyhow, this works as a resolution for me but is still I think an important question if in the case I was receiving dates in something like ''mm/dd/yyyy'' how I can get it to parse right. On Fri, Feb 4, 2011 at 11:32 AM, David Kahn <dk-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org>wrote:> I am getting the month and day reversed in parsing a date. I am wondering > if there is some intelligence in Ruby that knows that at the moment I am in > Mexico, as the parsing is working as if I was > > "6/15/2008".to_date > => ArgumentError: invalid date > > "15/6/2008".to_date > => Sun, 15 Jun 2008 > > What I really want is my date to be understood as month/day/year. Is there > a way to tell Ruby or Rails to do this? Been poking around on google and do > not see an answer that works here. > > I found a page with this example, which makes me think maybe there is > something weird going on due to my geographic location: > > ''06/15/2008''.to_date <http://apidock.com/rails/ActiveSupport/CoreExtensions/String/Conversions/to_date> # => Sun, 15 Jun 2008 > > > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 4 Feb 2011, at 17:36, David Kahn <dk-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org> wrote:> Ok, feel a little stupid, if I put the date in the form yyyy-mm-dd and then parse it I get the right result. Anyhow, this works as a resolution for me but is still I think an important question if in the case I was receiving dates in something like ''mm/dd/yyyy'' how I can get it to parse right. >You should probably use something like strptime rather than rely on ruby guessing the format (which could be product of locale settings, heuristics like ''if the first number is 15 then it can''t be the month'' and who knows what else Fred> > On Fri, Feb 4, 2011 at 11:32 AM, David Kahn <dk-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org> wrote: > I am getting the month and day reversed in parsing a date. I am wondering if there is some intelligence in Ruby that knows that at the moment I am in Mexico, as the parsing is working as if I was > > "6/15/2008".to_date > => ArgumentError: invalid date > > "15/6/2008".to_date > => Sun, 15 Jun 2008 > > What I really want is my date to be understood as month/day/year. Is there a way to tell Ruby or Rails to do this? Been poking around on google and do not see an answer that works here. > > I found a page with this example, which makes me think maybe there is something weird going on due to my geographic location: > > ''06/15/2008''.to_date # => Sun, 15 Jun 2008 > > > > > > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Might wanna look at : https://github.com/adzap/timeliness https://github.com/adzap/validates_timeliness Robert Pankowecki http://robert.pankowecki.pl -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
you might want to create a file: config/initializers/date_formats.rb and add the following line to that file... Date::DATE_FORMATS[:default] = "%m/%d/%Y" to set a default date format On Feb 4, 1:01 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 4 Feb 2011, at 17:36, David Kahn <d...-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org> wrote: > > > Ok, feel a little stupid, if I put the date in the form yyyy-mm-dd and then parse it I get the right result. Anyhow, this works as a resolution for me but is still I think an important question if in the case I was receiving dates in something like ''mm/dd/yyyy'' how I can get it to parse right. > > You should probably use something like strptime rather than rely on ruby guessing the format (which could be product of locale settings, heuristics like ''if the first number is 15 then it can''t be the month'' and who knows what else > > Fred > > > > > > > > > > > On Fri, Feb 4, 2011 at 11:32 AM, David Kahn <d...-rfEMNHKVqOwNic7Bib+Ti9h4dv9xpEYq@public.gmane.orgm> wrote: > > I am getting the month and day reversed in parsing a date. I am wondering if there is some intelligence in Ruby that knows that at the moment I am in Mexico, as the parsing is working as if I was > > > "6/15/2008".to_date > > => ArgumentError: invalid date > > > "15/6/2008".to_date > > => Sun, 15 Jun 2008 > > > What I really want is my date to be understood as month/day/year. Is there a way to tell Ruby or Rails to do this? Been poking around on google and do not see an answer that works here. > > > I found a page with this example, which makes me think maybe there is something weird going on due to my geographic location: > > > ''06/15/2008''.to_date # => Sun, 15 Jun 2008 > > > -- > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Hi, Take a look at strftime, you can convert to any time format by doing string.to_date.strftime(desired format) for example lets say that you have "2011/12/31" and you want to conver it to "12-31-11" you would do "2011/12/31".to_date..strftime("%d-%m-%Y") Jaz On Thu, Feb 24, 2011 at 10:07 AM, Teddyted <teddyted-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> you might want to create a file: config/initializers/date_formats.rb > and add the following line to that file... > Date::DATE_FORMATS[:default] = "%m/%d/%Y" > > to set a default date format > > On Feb 4, 1:01 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > On 4 Feb 2011, at 17:36, David Kahn <d...-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org> wrote: > > > > > Ok, feel a little stupid, if I put the date in the form yyyy-mm-dd and > then parse it I get the right result. Anyhow, this works as a resolution for > me but is still I think an important question if in the case I was receiving > dates in something like ''mm/dd/yyyy'' how I can get it to parse right. > > > > You should probably use something like strptime rather than rely on ruby > guessing the format (which could be product of locale settings, heuristics > like ''if the first number is 15 then it can''t be the month'' and who knows > what else > > > > Fred > > > > > > > > > > > > > > > > > > > > > On Fri, Feb 4, 2011 at 11:32 AM, David Kahn < > d...-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org> wrote: > > > I am getting the month and day reversed in parsing a date. I am > wondering if there is some intelligence in Ruby that knows that at the > moment I am in Mexico, as the parsing is working as if I was > > > > > "6/15/2008".to_date > > > => ArgumentError: invalid date > > > > > "15/6/2008".to_date > > > => Sun, 15 Jun 2008 > > > > > What I really want is my date to be understood as month/day/year. Is > there a way to tell Ruby or Rails to do this? Been poking around on google > and do not see an answer that works here. > > > > > I found a page with this example, which makes me think maybe there is > something weird going on due to my geographic location: > > > > > ''06/15/2008''.to_date # => Sun, 15 Jun 2008 > > > > > -- > > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > > For more options, visit this group athttp:// > groups.google.com/group/rubyonrails-talk?hl=en. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- Jazmin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.