How to correctly assign a section setting for date calculation?

How to correctly assign a section setting for date calculation?

Maria80
Shopify Partner
6 0 0

Hello! I want to display the days between a set date from section settings to today, and the assign function is not reading the id, would appriciate the help!

 

{%- assign dateStart = section.settings.date_started | date: '%s' -%}

{%- assign nowTimestamp = 'now' | date: '%s' -%}

{%- assign diffSeconds = nowTimestamp | minus: dateStart -%}

{%- assign diffDays = diffSeconds | divaded_by: 3600 | divided_by: 24 -%}

AND THEN I TRY TO DISPLAY IT

<div id="day-container" class="day-count-text">D{{ dateStart | t }}</div>
Replies 9 (9)

Finer
Shopify Partner
2672 559 933

@Maria80 I think the following Code was your basis

{% assign dateStart = product.created_at | date: '%s' %}
{% assign nowTimestamp = 'now' | date: '%s' %}

{% comment %} difference in seconds {% endcomment %}
{% assign diffSeconds = nowTimestamp | minus: dateStart %}

{% comment %} difference in days {% endcomment %}
{% assign diffDays = diffSeconds | divided_by: 3600 | divided_by: 24 %}

<p>difference in days = {{ diffDays }}</p>

 

In your Code you have a few minor typos (e.g., "divaded_by" -> "divided_by") and in the container you are referencing the dateStart value and not the diffDays value. Furthermore, you inserted a translation-key filter to {{ dateStart | t }}. You can simply write {{ diffDays }}

{%- assign dateStart = section.settings.date_started | date: '%s' -%}
{%- assign nowTimestamp = 'now' | date: '%s' -%}
{%- assign diffSeconds = nowTimestamp | minus: dateStart -%}
{%- assign diffDays = diffSeconds | divided_by: 3600 | divided_by: 24 -%}

AND THEN I TRY TO DISPLAY IT

<div id="day-container" class="day-count-text">{{ diffDays }}</div>

 

I am not 100% sure if this will work, but from the looks this should do the job.

- Did my answer help? Mark my post with a like
- Did I solve your problem? Mark my post as an accepted solution.
- You need professional help? Contact our Shopify Partner Agency
Maria80
Shopify Partner
6 0 0

Hey! Thank you for your quick response, I fixed all the issues you've mention, but now I get only 0 instead of days, do you know what could have caused that? Screenshot 2022-12-12 120339.png

Finer
Shopify Partner
2672 559 933

What kind of Input are you using?

Since there is no date input in the schema of a section, I assume you are using a number?

 

You can check the Input options here:

https://shopify.dev/themes/architecture/settings/input-settings

 

 

- Did my answer help? Mark my post with a like
- Did I solve your problem? Mark my post as an accepted solution.
- You need professional help? Contact our Shopify Partner Agency
Maria80
Shopify Partner
6 0 0

I use text input 🙂 and I wrote the date like this 12/10/2022

Maria80
Shopify Partner
6 0 0

I inserted" | t " next to the id, now it is readable by the liquid, but it shows me a wrong number 😞 

 

Maria80_0-1670843409678.png

Screenshot 2022-12-12 121021.png

Finer
Shopify Partner
2672 559 933

@Maria80 unfortunately, Shopify will not recognize that as a date. 

What is the general idea/goal behind this code? Do you want to add a countdown-timer?

- Did my answer help? Mark my post with a like
- Did I solve your problem? Mark my post as an accepted solution.
- You need professional help? Contact our Shopify Partner Agency
Maria80
Shopify Partner
6 0 0

Oh okay! so the goal is to calculate when someone started a challenge, so if they started on 01/12/2022, I would like it to display "D12". and then calculate it until it's day 185, then there is gonna be an icon with a check.

Finer
Shopify Partner
2672 559 933

Where do you want to display this information?

On the Homepage or on product page?

- Did my answer help? Mark my post with a like
- Did I solve your problem? Mark my post as an accepted solution.
- You need professional help? Contact our Shopify Partner Agency
Maria80
Shopify Partner
6 0 0

It is a seperate page called memebers, the section you saw with socials and a text from people who are doing the challenge 🙂