Date metafield formatting not working

Solved
DevBijan
Excursionist
21 0 7

Hi all, I have a date metafield on my products but when I follow the formatting rules given in the Shopify docs, nothing changes.

 

{{ class.metafields.my_fields.class_date | date: "%a, %b %d, %Y" }}

 

 

This should result in something like 

Sat, Oct 30, 2021

but all I get is 

2021-10-18T17:00:00Z

 

Am I doing something wrong? Any help would be appreciated, thanks!

Accepted Solution (1)

Accepted Solutions
mattrose
Shopify Staff
12 4 5

This is an accepted solution.

Hey @DevBijan, this is Matt from the Metafields team.

You're right, this is odd—piping a ISO-8601 string to the date filter should output the formatted date, like you've tried. I'll look into why this isn't working as expected. Can you point me to the docs that outlined this example?

In the meantime, you have a couple options. The first is access the date object by appending .value. This will return the date object, and allow you to format the date:

{{ product.metafields.my_fields.class_date.value | date: "%a, %b %d, %Y" }}

The other option is piping the metafield through the metafield_tag filter. This will automatically format the date using semantic HTML, but it won't give the fine grained control you're probably looking for.

{{ product.metafields.my_fields.class_date | metafield_tag }}

 

 

I hope this helps!

View solution in original post

Replies 8 (8)
mattrose
Shopify Staff
12 4 5

This is an accepted solution.

Hey @DevBijan, this is Matt from the Metafields team.

You're right, this is odd—piping a ISO-8601 string to the date filter should output the formatted date, like you've tried. I'll look into why this isn't working as expected. Can you point me to the docs that outlined this example?

In the meantime, you have a couple options. The first is access the date object by appending .value. This will return the date object, and allow you to format the date:

{{ product.metafields.my_fields.class_date.value | date: "%a, %b %d, %Y" }}

The other option is piping the metafield through the metafield_tag filter. This will automatically format the date using semantic HTML, but it won't give the fine grained control you're probably looking for.

{{ product.metafields.my_fields.class_date | metafield_tag }}

 

 

I hope this helps!

View solution in original post

DevBijan
Excursionist
21 0 7

I started here under Date Filters which led me to the Ruby formatting guide which didn't work, so I looked around more and found this blog post which just kind of confirmed for me that I wasn't doing anything noticeably wrong.

 

Thank you for your response, using .value worked!

DevBijan
Excursionist
21 0 7

Hi, I noticed that the time being displayed is 4 hours ahead of what the metafield says, do you have any idea why?

 

Product metafield:

DevBijan_1-1634577201702.png

 

Output:

 

DevBijan_0-1634577157357.png

 

mattrose
Shopify Staff
12 4 5

Interesting—it could be that different timezones are being used. If you use %Z in your format, it will print out the timezone being used. Both the admin and your liquid storefront should be using the shops preferred timezone. If you share your store name, I can investigate further.

DevBijan
Excursionist
21 0 7

The output is being shown as UTC but the store admin is set to use Eastern time. The store is currently under development and I can't share it right now, sorry.

 

DevBijan_0-1634651162277.png

DevBijan_1-1634651176761.png

DevBijan_2-1634651350486.png

My code:

{% assign classes = collection.products %}

<div class="class-id">
  {% for class in classes %}
  <div style="border:1px solid black;">
    <p>{{ class.title }}</p>
    <p>{{ class.metafields.my_fields.class_date.value | date: "%A, %B %e, %Y at %l:%M %p %Z" }}</p>
  </div>
  {% endfor %}
</div>

 

 

mattrose
Shopify Staff
12 4 5

Thanks for sharing @DevBijan.

I can confirm that this is a bug. Date Metafields aren't respecting the stores timezone. I've opened up an issue to fix this asap.

In the meantime, as a work around, you can append an empty string before formatting the date—this will convert the metafield to a string, which will respect the shop's timezone as expected.

{{ class.metafields.my_fields.class_date.value | append: "" | date: "%A, %B %e, %Y at %l:%M %p %Z" }}

 
Thanks so much for taking the time to report this.

DevBijan
Excursionist
21 0 7

Thanks for the response, this works! And no problem, I'm glad I could contribute!

 

One quick final question, is there a simple way of sorting an array by a product metafield date? I've looked around and haven't found any working solution. All good if not, thanks!

mattrose
Shopify Staff
12 4 5

One quick final question, is there a simple way of sorting an array by a product metafield date? I've looked around and haven't found any working solution. All good if not, thanks!


Unfortunately there isn't a simple way to sort by metafield values. However, I could see how useful it would be to dig through attributes using the sort filter. Something like {{ collection.products | sort: "metafields.my_fields.my_date" }}. I'll relay this recommendation to the team. Thanks!