I need to be able to list all Items in a Custom Module that were created within the last 14 days. That is 14 days from whenever the page is loaded. I also need to display that date, 14 days ago, whatever it was.
I can find all the Items and list them by converting the current date to ‘seconds since 1970’ & subtracting 14 days (14 * 24 * 60 * 60).
I’ve tried all sorts of different conversion strings that are supposed to work with Shopify Liquid without success.
The code doesn’t throw an error - it just renders nothing.
This issue is on the public backlog.
Does anyone know of a work around until this is provided?
The ugly solution for me at the moment is to compare the release dates & find the earliest that meets the last 14 days criterion - crude and not particularly exact.
I think until we get date comparisons in Liquid its probably not possible. But what about using Javascript?
Is newStartUnix simply the date 14 days ago? is that what you want to display?
In this case using JS something like in this article (but minus your 14 days in seconds):
Or for more advanced date stuff, use moment.js if it’s just for display purposes.
But maybe I’ve missed something… working with dates confuses me
Thats exactly what I’m doing at the moment Adam. I have the date comparison working properly in Liquid. Once the date is in seconds it’s just a numeric comparison. The date (2 weeks ago) displayed on the page doesn’t need to be rendered by Liquid. But it should come from the same calculation so any change to the comparison updates the list of Items as well as the heading.
Ah, cool.
The JS could still be feed the liquid variable for the 14 days (or whatever it might change to) so it could all still work together from the same calcs I guess.
Hey @James. I’ll flag @Peter-Schmidt in on this for any backlog adjustments.
I’m sure all the Liquid date manipulation stuff is on Treepl’s internal roadmap anyway, but just in case let’s make it super clear we need some better date functionality.
@peter.medbury It’s not exactly what you’re looking for but it might be useful in your quest.
I’m using it to calculate the days ago for blog posts http://prntscr.com/mybpup
{% comment %} convert our dates to Number of seconds since 1970-01-01 00:00:00 UTC {% endcomment %}
{% assign dateStart = this['ReleaseDate'] | date: '%s' %}
{% assign nowTimestamp = 'now' | date: '%s' %}
code in the Custom Module List Layout to do the comparison:
{% assign thenInSeconds = this[‘ReleaseDate’] | date: ‘%s’ | plus: 0 %}
{% if thenInSeconds > checkDateInSeconds -%} Item data to be displayed
{% endif %}
Javacript to show the date, ‘x’ days ago, that the Item Release Date was checked against:
function checkdate() {
var monthNames = [“January”, “February”, “March”, “April”, “May”, “June”,
“July”, “August”, “September”, “October”, “November”, “December”
];
var d = new Date();
d.setDate(d.getDate() - {{intervalindays}});
var e = d.getDate();
var b = monthNames[d.getMonth()];
var Y = d.getFullYear();
if (e < 10) {
e = ‘0’ + e;
}
d = e + ‘-’ + b + ‘-’ + Y;
document.getElementById(“intervalDaysAgo”).innerHTML = d;
}
Hi @peter.medbury.
An update, which was initially missed in the v4.0 release notes, has partially addressed this issue.
I’ve now updated the release notes but the update is:
Now Liquid can convert numbers to date (interpreted as number of seconds from 1st Jan 1970).