Add variable support to date math
complete
Brad H (Tekhaus)
The date math addition is super useful -- {{ "now + 3 months" | date: "%Y-%m-%d %H:%M %z" }} -- but I want to be able to use a variable date, such as "order.created_at" in lieu of "now".
Use case: easily calculating future subscription dates for draft orders based on original order date (e.g. - recurring monthly subscription, original date 2021-01-31, would give 2021-02-28, 2021-03-31, 2021-04-30, etc. when using "+ 1 month").
Trying to take into account DST, time zone differences, and variable months is very difficult with the simple date tools provided by liquid.
Previously proposed having the duration be a filter (e.g. {{ order_created_at | plus: "1 month" | date: "%Y-%m-%d" }} but not sure whether that is feasible.
Isaac Bowen
complete
Landed. :)
https://mechanic.canny.io/changelog/new-extensions-to-the-date-filter
Isaac Bowen
this is getting closer :)
Isaac Bowen
musing out loud:
{% assign the_eleventh_hour_before_the_next_midmonth_s = "now" | date: plus: "30 days" | date: beginning_of_month: true, plus: "15 days", minus: "1 hour" | date: "%s" %}
Brad (Tekhaus)
Isaac Bowen keep musing! :) the circles I am going in literally right now, mucking around calculating valid date at X intervals... and can't even do any end of month tests with "now" until, well the end of a month :)
Isaac Bowen
the idea behind this syntax is to extend the
date
filter with keyword arguments corresponding to relevant methods in https://api.rubyonrails.org/classes/Date.htmlBrad (Tekhaus) Matt Scheurich does this cover y'all's use cases? any holes?
Brad (Tekhaus)
I think that is likely more than enough. My current use case is taking a subscription start date, adding X months. Making sure the calculated date is valid, and moving backwards if not (end of month scenarios).
Oddly I can't grok the Ruby docs, but if I can do the above with the new date parameters, then awesome!
{% assign sub_expiration_date = sub_start_date | date: plus: "4 months" %}
What would be assigned to "test" in the below line?
{% assign test = "2024-01-31" | date: plus: "1 month" %}
M
Matt Scheurich
Isaac Bowen good for me!
Isaac Bowen
Brad (Tekhaus) default
date
output would be in Shopify's usual "2018-06-02T21:35:20Z" format, I think, in the absence of a positional format argumentBrad (Tekhaus)
Isaac Bowen Noted. I think my question was around what actual date would be the result of adding "1 month" to Jan 31, 2024?
I would expect Feb 29, 2024, but perhaps that's not the case?
Isaac Bowen
Brad (Tekhaus) ahhhhh thank you for helping focus me :) yep, it'd be feb 29
M
Matt Scheurich
API similar to dayjs could be cool:
"now" | date_add: 1, unit: "day" | date: "%s"
"now" | date_subtract: 1, unit: "week" | date: "%s"
"2021-06-17T06:23:00+02:00" | date_start: "day" | date: "%FT%T%:z", tz: "UTC"
=> 2021-06-30T00:00:00Z
Get end of date/time unit from date](https://day.js.org/docs/en/manipulate/end-of):
"2021-06-17T06:23:00+02:00" | date_end: "month" | date: "%FT%T%:z", tz: "UTC"
=> 2021-06-30T00:00:00Z
Date filters would interpret input string as date (or based on special date keywords like
now
), then default output as ISO string (YYYY-MM-DDTHH:MM:SSZ) which then could be fed into date
filter to change date format output.Brad H (Tekhaus)
Wrote this in a task today...
{% assign now_s = "now" | date: "%s" | times: 1 %}
{% assign days_to_keep_products_s = days_to_keep_products | times: 86400 %}
{% assign created_at_threshold_s = now_s | minus: days_to_keep_products_s %}
{% assign created_at_threshold = created_at_threshold_s | date: "%Y-%m-%d" %}
...and was thinking how nice it would be to just do something like...
{% assign created_at_threshold = "now" | date_minus: days_to_keep_products, "days" %}
...or...
{% assign created_at_threshold = "now" | date_math: "minus", days_to_keep_products, "days" %}