Product - date added

Shopify Expert
49 0 12

Jason's awesome solution formats the date using a liquid filter: http://wiki.shopify.com/Date

date:'%Y%m%d'

There are many ways to output the date, and that one in particular is very good for using simple math to compare if 'now' is sooner or later than a published date. 

Its the same general idea as the javascript concept, just with liquid it happens on the server which is faster and likely preferred. 

@radiatorstudios
0 Likes
Excursionist
36 0 31

Hi Radiator,

I'm not gonna output/display the date on my website, so it shouldn't be much of a concern what date format it is right?

Anyway, i'm trying out the sorting of collection. It sorts according to Newest to Oldest but based on Created date. Is it possible to sort based on updated date? Such that items that are sold out & restock will be at the front. (Else will need to delete the items & re-create them again)

 

0 Likes
Shopify Expert
49 0 12

The date formatting isn't for display on your site, its formatted in such a way to calculate if something is 'new'. 

As for the sorting, there is a similar thread posted here: http://ecommerce.shopify.com/c/shopify-discussion/t/feature-request-edit-product-creation-date-13420... Currently there is no simple way to control the updated/published date of a product in the admin area. The easiest way to control what is new in a collection where things update often is to use manual sorting. May take a bit more time on the day to day to manage -  but you get total control and don't need to code anything.

@radiatorstudios
0 Likes
Highlighted
Excursionist
36 0 31

Hi Radiator,

Oh i see. Thanks :)

1 Like
Shopify Expert
49 0 12

Recently stumbled on http://momentjs.com/. Thought of this thread.

More than likely, manipulating dates via liquid ( server-side ) is ideal for your store. Jason's snippet is very close to a perfect solution.

However, should you need or prefer a client-side solution, this library can really simply the formatting and comparison of dates.

@radiatorstudios
1 Like
New Member
1 0 1

I was trying to do the same thing just now and found this thread. The problem with the solutions outlined above (unless I'm being thick, which is always a possibility) is accounting for all the edge cases. E.g. if a product is created on the last day of the year, the numeric difference when comparing dates in YYMMDD format will be 8870, whereas the difference between two products created on two sequential days in the same month will be 1. So you have to do a lot of if/else to make it work in all cases.

What I couldn't see documented anywhere is that Liquid seems to support the '%s' format directive to output a date as the number of seconds since Jan 1st 1970. This means you can do (date1 | Date:'%s') - (date2 | Date:'%s') and get a difference between the two in seconds, making life much easier as 1 day always equals (60*60*24) seconds regardless of what year/month boundaries it crosses.

Hope this helps someone!

1 Like