Hi, I have to store dates inside a DB, but unfortunately my date representation can be a string like "2007" but also a normal date string, like "2007-8-12" ... I want to use Date/DateTime objects because they are easy to manipulate, but Date.parse can parse only the second format, not the first (which is understandable ...). How can I solve this problem ? Storing something like 2007-0-0 or 2007-1-1 is ok for me, like in Date.new(2007). I can check if the string contains ''-'' and if not do something like Date.new(string.to_i) but I don''t like this solution ... TIA, ngw Chiacchiera con i tuoi amici in tempo reale! http://it.yahoo.com/mail_it/foot/*http://it.messenger.yahoo.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 -~----------~----~----~----~------~----~------~--~---
string = ''2007-01-01'' Date.new(*string.split(''-'').map(&:to_i)) On 10/9/06, Nicholas Wieland <nicholas_wieland-whZMOeQn8C0@public.gmane.org> wrote:> > > Hi, I have to store dates inside a DB, but unfortunately my date > representation can be a string like "2007" but also a normal date > string, like "2007-8-12" ... > I want to use Date/DateTime objects because they are easy to > manipulate, but Date.parse can parse only the second format, not the > first (which is understandable ...). > How can I solve this problem ? Storing something like 2007-0-0 or > 2007-1-1 is ok for me, like in Date.new(2007). > I can check if the string contains ''-'' and if not do something like > Date.new(string.to_i) but I don''t like this solution ... > > TIA, > ngw > Chiacchiera con i tuoi amici in tempo reale! > http://it.yahoo.com/mail_it/foot/*http://it.messenger.yahoo.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 -~----------~----~----~----~------~----~------~--~---
Il giorno 09/ott/06, alle ore 03:22, Jonathan Viney ha scritto:> string = ''2007-01-01'' > Date.new(*string.split(''-'').map(&:to_i))Thank you, but my problem is that my input is only ''2007'', if I had a string like ''2007-01-01'' I could use Date.parse :) ngw Chiacchiera con i tuoi amici in tempo reale! http://it.yahoo.com/mail_it/foot/*http://it.messenger.yahoo.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 -~----------~----~----~----~------~----~------~--~---
On Oct 9, 3:33 am, Nicholas Wieland <nicholas_wiel...-whZMOeQn8C0@public.gmane.org> wrote:> Il giorno 09/ott/06, alle ore 03:22, Jonathan Viney ha scritto: > > > string = ''2007-01-01'' > > Date.new(*string.split(''-'').map(&:to_i))Thank you, but my problem is that my input is only ''2007'', if I had a > string like ''2007-01-01'' I could use Date.parse :) > > ngw > Chiacchiera con i tuoi amici in tempo reale! > http://it.yahoo.com/mail_it/foot/*http://it.messenger.yahoo.comirb:> string = ''2007'' => "2007" irb:> Date.new(*string.split(''-'').map(&:to_i)) => 2007-01-01 This seems to actually do what you want it to... _Kevin --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---