Ordinalize Dates

Rob_Curry1
Shopify Partner
20 0 7

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
Rob_Curry1
Shopify Partner
20 0 7

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
Jamie
Shopify Staff (Retired)
Shopify Staff (Retired)
5711 0 152

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
Rob_Curry1
Shopify Partner
20 0 7

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
iamkeir
Shopify Partner
194 0 29

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
Rob_Curry1
Shopify Partner
20 0 7

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
rickydazla
Shopify Expert
690 0 73

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

I'm a million different people
0 Likes
iamkeir
Shopify Partner
194 0 29

Limitation precipitates innovation ;)

http://iamkeir.com
0 Likes
rickydazla
Shopify Expert
690 0 73

I'm using that too!

I'm a million different people
0 Likes
iamkeir
Shopify Partner
194 0 29

;)

http://iamkeir.com
0 Likes