Forum: Ruby on Rails How to get dd mmm and yyyy from dd-mmm-yyyy

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B72610c28afc18f4b817770c8b9accd8?d=identicon&s=25 Neeraj Kumar (Guest)
on 2006-06-05 19:12
(Received via mailing list)
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
39b36b2be47228f8619d61ea7a607a25?d=identicon&s=25 Matthew Beale (mixonic)
on 2006-06-05 19:15
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
7c4087d053eb02d099a17d91ba5e33b5?d=identicon&s=25 Brian Hughes (Guest)
on 2006-06-05 19:19
(Received via mailing list)
On Jun 5, 2006, at 01:15 PM, Matthew Beale wrote:
>> 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
Eea7ad39737b0dbf3de38874e0a6c7d8?d=identicon&s=25 Justin Forder (Guest)
on 2006-06-09 14:25
(Received via mailing list)
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
This topic is locked and can not be replied to.