Rubyx
Rubyx

Reputation: 798

Convert Date to Timestamp using Shopify Liquid

I building a template and I want to convert a date to a Timestamp on Shopify.

I know I can convert a timestamp to a date using this:

{{ '1560523384' | date: "%d-%m-%Y" }} // result is 14-06-2019

But I would like to do the opposite

{{ '14-06-2019' | timestamp }} // do not work

Anyway to do this using Shopify Liquid?

Upvotes: 2

Views: 13470

Answers (1)

Subhrajyoti Das
Subhrajyoti Das

Reputation: 2710

What you are looking at is Epoch Timestamp. To get the same use the below code in Liquid templates -

var timestamp = "{{ 'now' | date: "%s"}}";

This gives the current timestamp in Epoch Format. Here is a link to read more about date formatting in Liquid files - Link

Update

Here is an updated answer how to convert a sepcific date to epoch timestamp in shopify liquid code
var timestamp = "{{ "March 14, 2016" | date: "%s"}}";
# timespamp —- 1457928000

var timestamp = "{{ "14-06-2019" | date: "%s"}}";
# timespamp —- 1560484800

And vice versa from Timestap to Date

var _date = "{{ "now" | date: "%Y-%m-%d %H:%M" }}";
# _date —- 2021-08-24 01:14

var _date = "{{ "1560484800" | date: "%Y-%m-%d %H:%M" }}";
# _date —- 2019-06-14 00:00

You can so forth convert any timestamp in the date format in the above documentation link.

Upvotes: 7

Related Questions