# Format natural date

**URL:** <https://www.ruby-forum.com/t/format-natural-date/137232>\
**Category:** Ruby\
**Created:** [May 10, 2008, 1:03pm UTC](https://www.ruby-forum.com/t/format-natural-date/137232 "2008-05-10T13:03:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [May 10, 2008, 1:03pm UTC](https://www.ruby-forum.com/t/format-natural-date/137232/1 "2008-05-10T13: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

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [May 10, 2008, 2:41pm UTC](https://www.ruby-forum.com/t/format-natural-date/137232/2 "2008-05-10T14:41:00Z")

</div>

On 10.05.2008 13:03, John B. 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?

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

robert

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [May 10, 2008, 2:45pm UTC](https://www.ruby-forum.com/t/format-natural-date/137232/3 "2008-05-10T14:45:18Z")

</div>

On Sat, May 10, 2008 at 6:03 AM, John B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [May 10, 2008, 2:54pm UTC](https://www.ruby-forum.com/t/format-natural-date/137232/4 "2008-05-10T14:54:12Z")

</div>

Todd B. wrote:

> On Sat, May 10, 2008 at 6:03 AM, John B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> 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”)
