# How can I get the date in UK format: 11th January 2008

**URL:** <https://www.ruby-forum.com/t/how-can-i-get-the-date-in-uk-format-11th-january-2008/125662>\
**Category:** Rails\
**Created:** [January 11, 2008, 1:29am UTC](https://www.ruby-forum.com/t/how-can-i-get-the-date-in-uk-format-11th-january-2008/125662 "2008-01-11T01:29:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![theogb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bb73d2/32.png) [@theogb](https://www.ruby-forum.com/u/theogb)\
**Post date:** [January 11, 2008, 1:29am UTC](https://www.ruby-forum.com/t/how-can-i-get-the-date-in-uk-format-11th-january-2008/125662/1 "2008-01-11T01:29:32Z")

</div>

This sounds trivial but it’s annoying me.

The UK format for a full date would be Friday 11th January 2008 or 11th  
January 2008.

However, date.to\_s(:long) gives “January 11, 2008”

Is there a way to get different dates out from it?

Cheers  
Theo

---

<div class="post-metadata">

**Author:** ![theogb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bb73d2/32.png) [@theogb](https://www.ruby-forum.com/u/theogb)\
**Post date:** [January 11, 2008, 1:48am UTC](https://www.ruby-forum.com/t/how-can-i-get-the-date-in-uk-format-11th-january-2008/125662/2 "2008-01-11T01:48:53Z")

</div>

> This sounds trivial but it’s annoying me.
> 
> The UK format for a full date would be Friday 11th January 2008 or 11th  
> January 2008.
> 
> However, date.to\_s(:long) gives “January 11, 2008”
> 
> Is there a way to get different dates out from it?

See the strftime method. I believe there is a way to add formats into  
Rails “to\_s” extension as well, but you’d have to look into that. See  
ActiveSupport::CoreExtensions::Time::Conversions for more info.

time.strftime( string ) =\> string

Formats time according to the directives in the given format string. Any  
text not listed as a directive will be passed through to the output  
string.

Format meaning:

%a - The abbreviated weekday name (`Sun'') %A - The full weekday name (`Sunday’’)  
%b - The abbreviated month name (`Jan'') %B - The full month name (`January’’)  
%c - The preferred local date and time representation  
%d - Day of the month (01…31)  
%H - Hour of the day, 24-hour clock (00…23)  
%I - Hour of the day, 12-hour clock (01…12)  
%j - Day of the year (001…366)  
%m - Month of the year (01…12)  
%M - Minute of the hour (00…59)  
%p - Meridian indicator (`AM'' or`PM’’)  
%S - Second of the minute (00…60)  
%U - Week number of the current year,  
starting with the first Sunday as the first  
day of the first week (00…53)  
%W - Week number of the current year,  
starting with the first Monday as the first  
day of the first week (00…53)  
%w - Day of the week (Sunday is 0, 0…6)  
%x - Preferred representation for the date alone, no time  
%X - Preferred representation for the time alone, no date  
%y - Year without a century (00…99)  
%Y - Year with century  
%Z - Time zone name  
%% - Literal ``%’’ character

```
t = Time.now
t.strftime("Printed on %m/%d/%Y") #=> "Printed on 04/09/2003"
t.strftime("at %I:%M%p") #=> "at 08:56A
```

---

<div class="post-metadata">

**Author:** ![theogb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bb73d2/32.png) [@theogb](https://www.ruby-forum.com/u/theogb)\
**Post date:** [January 11, 2008, 1:52am UTC](https://www.ruby-forum.com/t/how-can-i-get-the-date-in-uk-format-11th-january-2008/125662/3 "2008-01-11T01:52:59Z")

</div>

To get the 11th you’ll need to use Inflector.ordinalize.

t = Time.now  
t.strftime(“%A”) + Inflector.ordinalize(t.strftime(“%d”)) + " " +  
t.strftime(“%B %Y”)

On Jan 11, 2008 1:37 PM, Philip H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> See the strftime method. I believe there is a way to add formats into  
> Format meaning:  
> %m - Month of the year (01…12)  
> %x - Preferred representation for the date alone, no time
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![theogb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bb73d2/32.png) [@theogb](https://www.ruby-forum.com/u/theogb)\
**Post date:** [January 11, 2008, 8:56am UTC](https://www.ruby-forum.com/t/how-can-i-get-the-date-in-uk-format-11th-january-2008/125662/4 "2008-01-11T08:56:06Z")

</div>

Cheers to both of you!

I’ll give this a try 🙂

---

<div class="post-metadata">

**Author:** ![theogb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bb73d2/32.png) [@theogb](https://www.ruby-forum.com/u/theogb)\
**Post date:** [January 14, 2008, 4:48pm UTC](https://www.ruby-forum.com/t/how-can-i-get-the-date-in-uk-format-11th-january-2008/125662/5 "2008-01-14T16:48:03Z")

</div>

Okay,

I looked about and I just need to put this sort of thing in the  
environment.rb file:

ActiveSupport::CoreExtensions::Time::Conversions::DATE\_FORMATS.merge!(  
:uk\_long =\> lambda { |time| time.strftime("%A #{time.day.ordinalize}  
%B %Y") }  
)

The long\_ordinal format already puts the ‘th’ or whatever after the day  
so I just altered the code in the API for that version.

Cheers  
Theo

---

<div class="post-metadata">

**Author:** ![theogb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bb73d2/32.png) [@theogb](https://www.ruby-forum.com/u/theogb)\
**Post date:** [January 15, 2008, 10:55am UTC](https://www.ruby-forum.com/t/how-can-i-get-the-date-in-uk-format-11th-january-2008/125662/6 "2008-01-15T10:55:26Z")

</div>

Theo Graham-brown wrote:

> Okay,
> 
> I looked about and I just need to put this sort of thing in the  
> environment.rb file:
> 
> ActiveSupport::CoreExtensions::Time::Conversions::DATE\_FORMATS.merge!(  
> :uk\_long =\> lambda { |time| time.strftime("%A #{time.day.ordinalize}  
> %B %Y") }  
> )
> 
> The long\_ordinal format already puts the ‘th’ or whatever after the day  
> so I just altered the code in the API for that version.
> 
> Cheers  
> Theo

Actually that solution didn’t work, but Ryan’s did. I’ll have to  
investigate further at some point.
