Question
How can I make a reference to "now" using a placeholder in a ticket update? I tried using {{ticket.updated_at}}
but it shows the time of the previous comment.
Answer
The update_at placeholders are processed as the comment text is being saved, so it only has access to the previous comment data. As a result, it is expected that it would render the time of the most recent comment.
However, you can use some default Liquid Markup functionality to render a timestamp of the moment a comment is processed. This is detailed at https://shopify.github.io/liquid/filters/date/ and would look like:
The current timestamp is {{ "now" | date: "%Y-%m-%d %H:%M" }}
The latter formatting can be changed as you prefer, using http://strftime.net/.
Cool! But it's showing the wrong time for my users
Good observation! The timestamp will always return in UTC, so if you are in a different timezone you would need to adjust it accordingly. The simplest way to do this is to convert the date into Unix time, add or subtract the appropriate time difference, and format it as you wish. For example, to display the time in EST, use the code below.
This article was updated at {{"now" | date: "%s" | minus : 18000 | date: "%Y-%m-%d %H:%M %P EST"}}
This would render the message below.
This article was updated at 2017-11-22 06:42 am EST
Important: The above solution is using an absolute reference to UTC. This solution goes out of sync in the long term when factors like Daylights Savings Time are taken into account. If you are in a region that observes DST, edit the placeholder according to when the difference to UTC changes.
2 Comments
Will show the time in the following format
Date: 11/09/18 Time: 20:29:04
This is for EST (without factoring in DST). You'd need to update the placeholder according to your timezone
To break it down:
Will show Date: 11/09/18 Time:
Creating the time needs a bit of work:
This subtracts 5 from the GMT hour. However, if it's 01:00 GMT, the result will be -4 (since 1 - 5 = -4), so we need to convert that to the correct number:
This will change -1 to 23, -2 to 22, -3 to 21, etc.. If the number isn't negative, then we will just keep the result of GMT -5:
With that in place, we will see just the hour, so we need to add the minutes and seconds.
This shows ":29:04"
Altogether, it'll look like this:
Date: 11/09/18 Time: 20:29:04
Thanks for sharing that, Mark!
Please sign in to leave a comment.