液体模板过滤器中的日期数学/操作 [英] Date Math / Manipulation in Liquid Template Filter

查看:125
本文介绍了液体模板过滤器中的日期数学/操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Desk.com中构建一个集成URL",它使用Shopify液体模板过滤器语法.该网址需要包含一个开始日期"和一个结束日期"才能用于查询,该查询的开始日期为7天前,而结束日期为现在.

I'm constructing an "Integration URL" in Desk.com, which uses the Shopify Liquid Template filter syntax. This URL needs to contain a "start date" and an "end date" for a query where the start date is 7 days ago and the end date is right now.

要实现这一点,我认为我需要从现在"对象中减去7天(在Epoch时间中为604800),然后应用我的格式设置,但我无法为此找到有效的语法.

To achieve this I think I need to subtract 7 days (604800 in Epoch time) from the 'now' object and then apply my formatting but I can't figure out valid syntax for that.

当前,此语法有效且有效:

For the current time, this syntax is valid and working:

{{'now' | date: "%b %d, %Y %I:%M %p -0500" | uri_encode | replace:"+","%20"}}

7天前,这是我能想到的最好的方法(不起作用):

For 7 days ago, here's the best I could come up with (isn't working):

{{'now' | minus : 604800 | date: "%b %d, %Y %I:%M %p -0500" | uri_encode | replace:"+","%20"}}

在Liquid中对"7天前"的有效语法有任何建议吗?非常感谢任何建议!

Any suggestions on a valid syntax for "7 days ago" in Liquid? Would greatly appreciate any advice!

推荐答案

非常感谢Desk.com的 @iveskev "WOW"小组为此提供了答案:

Much thanks to @iveskev from the Desk.com "WOW" team for this answer:

如果执行{{'now'}},它将返回字符串"now",而不是当前时间的时间戳.因此,如果执行{{'now' | minus: 604800 }},它将返回"-604800",而不是当前的unix时间减去604800.使用date过滤器时,液体会拾取您正在引用的当前时间,并将时间输出为字符串.但是,即使我们获得"now"来输出当前日期,我们仍然从字符串中减去,因此将返回"-604800".字符串上的数学运算唯一可以正常运行的情况是,字符串是否只有数字.

If you do {{'now'}} it returns the string "now" not a timestamp for the current time. So if you do {{'now' | minus: 604800 }} it returns "-604800" not the current unix time minus 604800. When you use the date filter, then liquid picks up that you are referencing the current time and outputs the time as a string. However even if we get ‘now’ to output the current date, we are still subtracting from a string and so will be returned with "-604800". The only time that math on a string works correctly is if the sting is only a number.

因此,为了获得正确的日期,我们首先必须现在获取unix时间戳,进行减法,然后重新格式化为所需的格式.您可以使用%s获取Unix时间.因此,要获得当前的Unix时间,它将是: {{'now' | date: '%s' }}

So in order to get the correct date we first have to get the unix timestamp for now, do the subtraction, then reformat to the desired formate. You can use %s to get unix time. So to get the current time in unix it would be: {{'now' | date: '%s' }}

这时,您可以进行减法,然后以正确的方式格式化时间.我们可以在以下语句中一次完成所有操作:

At that point you can then do the subtraction and then format the time in the correct way. We can do this all at once in the following statement:

{{'now' | date: "%s" | minus : 604800 | date: "%b %d, %Y %I:%M %p -0500" | uri_encode | replace:"+","%20"}}

这篇关于液体模板过滤器中的日期数学/操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆