Ordinalize Dates

Highlighted
Shopify Partner
20 0 6

Hi,

Is there any way you can add the facility to ordinalize the dates when formatting i.e. adding the nd, th, rd, and st suffixes?

Whilst this might not bother too many people, i think it would come in very handy. I'm maybe assuming there is a reason for not currently supporting it?

Cheers.

 

Rob Curry - Shopify Expert / Web Developer @ RyanFosterDesign Ltd
0 Likes
Shopify Partner
20 0 6

Since there seems to be no built-in function to do this i had to resort to wrapping the "day" in a <span> and then using javascript to determine the ordinal and update. Not ideal but it works!

Rob Curry - Shopify Expert / Web Developer @ RyanFosterDesign Ltd
0 Likes
Shopify Staff (Retired)
Shopify Staff (Retired)
2000 0 117

You can try using the replace filter to ordinalize like so:

 

{{ article.created_at | date: "%d " | replace: '1', '1st'  | replace: '2', '2nd' }}

You may need to experiment here and use a mix of replace_first and replace filters to get this 100%. If you do go that route please post back with your solution for others.

 

http://shopifyplus.com ::: http://twitter.com/bacchus
0 Likes
Shopify Partner
20 0 6

Hi Jamie,

thanks for the suggestion, i think that will be a rather long winded approach to be honest. as it would obviously involve 31 replaces.

I think i'll stick with my javascript solution until a built-in formatting option is available.

Rob Curry - Shopify Expert / Web Developer @ RyanFosterDesign Ltd
0 Likes
Shopify Partner
189 0 27

Hi Rob/Jamie,

Can be done in 9 replaces as follows but it ain't pretty...

{{ article.published_at | date: '~%dth %B %Y' | replace: '~0', '' | replace: '~', '' | replace: '1th', '1st' | replace: '2th', '2nd' | replace: '3th', '3rd' | replace: '11st', '11th' | replace: '12nd', '12th' | replace: '13rd', '13th' }}

It would be a lot simpler if an ordinal existed in Liquid!

 

http://iamkeir.com
0 Likes
Shopify Partner
20 0 6

Thanks for that little snippet. Might come in handy but yeah, an ordinal within liquid would be SO much easier!

Rob Curry - Shopify Expert / Web Developer @ RyanFosterDesign Ltd
0 Likes
Shopify Expert
682 0 67

@iamkeir -- 'kin genius bizniss :) LOVE IT

I'm a million different people
0 Likes
Shopify Partner
189 0 27

Limitation precipitates innovation ;)

http://iamkeir.com
0 Likes
Shopify Expert
682 0 67

I'm using that too!

I'm a million different people
0 Likes
Shopify Partner
189 0 27

;)

http://iamkeir.com
0 Likes