The input values could be something like this: 01-Dec-2006 01-December-2006 1-June-2006 Is there an easy to way to get the three variables populated dd, mmm and yyyy for any of the above input values? I tried to look at regex but couldn'' anything simple. Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060605/144caa21/attachment.html
Matthew Beale
2006-Jun-05 17:15 UTC
[Rails] Re: How to get dd mmm and yyyy from dd-mmm-yyyy
Neeraj Kumar wrote:> The input values could be something like this: > 01-Dec-2006 > 01-December-2006 > 1-June-2006 > > Is there an easy to way to get the three variables populated dd, mmm and > yyyy for any of the above input values? I tried to look at regex but > couldn'' > anything simple. >http://ruby-doc.org/core/classes/String.html#M001875 ''01-December-2006''.split ''-'' -- Matthew Beale :: mixonic@synitech.com Resume & Portfolio @ http://madhatted.com -- Posted via http://www.ruby-forum.com/.
Brian Hughes
2006-Jun-05 17:18 UTC
[Rails] Re: How to get dd mmm and yyyy from dd-mmm-yyyy
On Jun 5, 2006, at 01:15 PM, Matthew Beale wrote:> Neeraj Kumar wrote: >> The input values could be something like this: >> 01-Dec-2006 >> 01-December-2006 >> 1-June-2006 >> >> Is there an easy to way to get the three variables populated dd, >> mmm and >> yyyy for any of the above input values? I tried to look at regex but >> couldn'' >> anything simple. >> > > http://ruby-doc.org/core/classes/String.html#M001875 > > ''01-December-2006''.split ''-''Which would return an Array object. If you wanted to easily have the values sent to your three variables, you can do: dd, mmm, yyyy = ''01-December-2006''.split ''-'' -Brian
Neeraj Kumar wrote:> The input values could be something like this: > 01-Dec-2006 1-June-2006 > 01-December-2006 > 1-June-2006 > > Is there an easy to way to get the three variables populated dd, mmm and > yyyy for any of the above input values? I tried to look at regex but > couldn'' > anything simple.Hi, sorry for the late reply. ParseDate.parsedate(string) will do what you are looking for. There''s some documentation here: http://www.rubycentral.com/book/lib_standard.html (open the page and then find parsedate) For example, in irb: irb(main):004:0> require ''parsedate'' => false irb(main):005:0> s1 = ''01-Dec-2006'' => "01-Dec-2006" irb(main):006:0> s2 = ''01-December-2006'' => "01-December-2006" irb(main):007:0> s3 = ''1-June-2006'' => "1-June-2006" irb(main):008:0> a1 = ParseDate.parsedate(s1) => [2006, 12, 1, nil, nil, nil, nil, nil] irb(main):009:0> y1, m1, d1 = ParseDate.parsedate(s1) => [2006, 12, 1, nil, nil, nil, nil, nil] irb(main):010:0> y1 => 2006 irb(main):011:0> m1 => 12 irb(main):012:0> d1 => 1 irb(main):013:0> a2 = ParseDate.parsedate(s2) => [2006, 12, 1, nil, nil, nil, nil, nil] irb(main):014:0> a3 = ParseDate.parsedate(s3) => [2006, 6, 1, nil, nil, nil, nil, nil] regards Justin