# How to get dd mmm and yyyy from dd-mmm-yyyy

**URL:** <https://www.ruby-forum.com/t/how-to-get-dd-mmm-and-yyyy-from-dd-mmm-yyyy/62513>\
**Category:** Rails\
**Created:** [June 5, 2006, 7:12pm UTC](https://www.ruby-forum.com/t/how-to-get-dd-mmm-and-yyyy-from-dd-mmm-yyyy/62513 "2006-06-05T19:12:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nekapoor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ea666f/32.png) [@nekapoor](https://www.ruby-forum.com/u/nekapoor)\
**Post date:** [June 5, 2006, 7:12pm UTC](https://www.ruby-forum.com/t/how-to-get-dd-mmm-and-yyyy-from-dd-mmm-yyyy/62513/1 "2006-06-05T19:12:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nekapoor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ea666f/32.png) [@nekapoor](https://www.ruby-forum.com/u/nekapoor)\
**Post date:** [June 5, 2006, 7:15pm UTC](https://www.ruby-forum.com/t/how-to-get-dd-mmm-and-yyyy-from-dd-mmm-yyyy/62513/2 "2006-06-05T19:15:23Z")

</div>

Neeraj K. 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](http://ruby-doc.org/core/classes/String.html#M001875)

‘01-December-2006’.split ‘-’

–  
Matthew B. :: removed\_email\_address@domain.invalid  
Resume & Portfolio @ [http://madhatted.com](http://madhatted.com)

---

<div class="post-metadata">

**Author:** ![nekapoor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ea666f/32.png) [@nekapoor](https://www.ruby-forum.com/u/nekapoor)\
**Post date:** [June 5, 2006, 7:19pm UTC](https://www.ruby-forum.com/t/how-to-get-dd-mmm-and-yyyy-from-dd-mmm-yyyy/62513/3 "2006-06-05T19:19:13Z")

</div>

On Jun 5, 2006, at 01:15 PM, Matthew B. wrote:

> > anything simple.
> 
> [class String - RDoc Documentation](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

---

<div class="post-metadata">

**Author:** ![nekapoor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ea666f/32.png) [@nekapoor](https://www.ruby-forum.com/u/nekapoor)\
**Post date:** [June 9, 2006, 2:25pm UTC](https://www.ruby-forum.com/t/how-to-get-dd-mmm-and-yyyy-from-dd-mmm-yyyy/62513/4 "2006-06-09T14:25:42Z")

</div>

Neeraj K. 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](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
