# Format natural date

**URL:** <https://rubytalk.org/t/format-natural-date/46423>\
**Category:** ruby-talk\
**Created:** [10 May 2008 11:03 UTC](https://rubytalk.org/t/format-natural-date/46423 "2008-05-10T11:03:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Butler](https://avatars.discourse-cdn.com/v4/letter/j/c4cdca/32.png) [@John\_Butler](https://rubytalk.org/u/John_Butler)\
**Post date:** [10 May 2008 11:03 UTC](https://rubytalk.org/t/format-natural-date/46423/1 "2008-05-10T11:03:18Z")

</div>

Is there a way to format a natural date like May 7, 2008 to 07/05/2008.  
Ive tried  
Date.strptime("May 7, 2008", '%d/%m/%Y') without success?

Is it possible to this in a one line with ruby?

JB

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [10 May 2008 12:40 UTC](https://rubytalk.org/t/format-natural-date/46423/2 "2008-05-10T12:40:02Z")

</div>

> Is there a way to format a natural date like May 7, 2008 to 07/05/2008.  
> Ive tried  
> Date.strptime("May 7, 2008", '%d/%m/%Y') without success?

You seem to want to \_reformat\_ a date string. But the code you show just \_parses\_ a string into a Date. If you want to reformat your string you first need to parse it and then format it.

> Is it possible to this in a one line with ruby?

Yes. You're almost there but you need to format your format string so it matches the date strings that you are trying to parse. Since there is nowhere a slash in your date string it comes as no surprise that the format does not match. 🙂

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 10.05.2008 13:03, John Butler wrote:

---

<div class="post-metadata">

**Author:** ![Todd\_Benson](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@Todd\_Benson](https://rubytalk.org/u/Todd_Benson)\
**Post date:** [10 May 2008 12:44 UTC](https://rubytalk.org/t/format-natural-date/46423/3 "2008-05-10T12:44:31Z")

</div>

Yes, but I don't know why you would want to.

#strptime builds a Date object from your string with your format.  
Then, you must format it.

If you don't need the Date object, I would just perform the magic on  
the string itself. Matching month abbreviations to month numbers can  
be done with Date::ABBR\_MONTHNAMES.

hth,

Todd

> **···**
>
> On Sat, May 10, 2008 at 6:03 AM, John Butler \<johnnybutler7@gmail.com\> wrote:
> 
> > Is there a way to format a natural date like May 7, 2008 to 07/05/2008.  
> > Ive tried  
> > Date.strptime("May 7, 2008", '%d/%m/%Y') without success?
> > 
> > Is it possible to this in a one line with ruby?
> > 
> > JB

---

<div class="post-metadata">

**Author:** ![John\_Butler](https://avatars.discourse-cdn.com/v4/letter/j/c4cdca/32.png) [@John\_Butler](https://rubytalk.org/u/John_Butler)\
**Post date:** [10 May 2008 12:54 UTC](https://rubytalk.org/t/format-natural-date/46423/4 "2008-05-10T12:54:12Z")

</div>

Todd Benson wrote:

> **···**
>
> > On Sat, May 10, 2008 at 6:03 AM, John Butler \<johnnybutler7@gmail.com\> \> wrote:
> > 
> > > Is there a way to format a natural date like May 7, 2008 to 07/05/2008.  
> > > Ive tried  
> > > Date.strptime("May 7, 2008", '%d/%m/%Y') without success?
> > > 
> > > Is it possible to this in a one line with ruby?
> > > 
> > > JB
> > 
> > Yes, but I don't know why you would want to.
> > 
> > #strptime builds a Date object from your string with your format.  
> > Then, you must format it.
> > 
> > If you don't need the Date object, I would just perform the magic on  
> > the string itself. Matching month abbreviations to month numbers can  
> > be done with Date::ABBR\_MONTHNAMES.
> > 
> > hth,
> > 
> > Todd
> 
> Ok i see, i didnt explain myself properly, i wanted to turn "May 23,  
> 2008" into a date and then output it as 23/05/2008"
> 
> Below works fine.
> 
> Date.strptime("May 23, 2008", '%B %d, %Y').strftime("%d/%m/%Y")  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
