简体   繁体   中英

Liquid : How To Do Math Operations On Date?

I'm using liquid inside a Microsoft CRM Portal. I want to get the seconds from the date and then do some operations with them, but I'm experiencing a few issues.

  1. In order to get the seconds according to this site : I have to use the filter "%S", but when I do that all I get is the capital letter S. According to the same site if I use the filter "%s" I should get the time in seconds from the epoch, but that actually gives me the seconds. So I'm using the line:

    {% assign seconds = "now" | date:"%s" %}

  2. If I try to use the operation plus: on the result, instead of adding 5, it concatenates 5 at the end. According to this post date returns a string. (In the same, post it also says that math should work on a string if it is only a number and that "%s" should return the unix time, but I can't get these parts to work).

  3. According to this post a string can be converted to an integer by multiplying it by 1 or adding 0. Adding 0 doesn't work, because it concatenates it at the end. On the other hand multiplying by 1 seems to work, but then if I try to add a number to the result, I get an error message: "Liquid error: Parameter count mismatch.

Here is the full code:

{% assign seconds = "now" | date:"%s" %}
{% assign test1 = seconds | plus:5 %}
<p>{{test1}}</p>
{% assign test2 = seconds | times:1 %}
<p>{{test2}}</p>
{% assign test3 = test2 | plus:10 %}
<p>{{test3}}</p>

and Here is the output:

305
30
Liquid error: Parameter count mismatch

I'm pretty sure, that I'm doing something wrong, but I can't figure out what. I would really appreciate it if someone could help me figure it out.

Thank you

Try

{% assign seconds = now | date: '%s' | integer %}
{% assign test1 = seconds | plus: 5 %}
  • now doesn't need brackets and shouldn't be in quotes
  • 's' represents seconds
  • integer casts the result to a number

The "s" custom format specifier represents the seconds as a number from 0 through 59. The result represents whole seconds that have passed since the last minute. A single-digit second is formatted without a leading zero.

References

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM