Skip to content

Instantly share code, notes, and snippets.

@jpvalery
Last active August 31, 2020 12:41
Show Gist options
  • Save jpvalery/dfcfe219267e5914a5d61399f3724057 to your computer and use it in GitHub Desktop.
Save jpvalery/dfcfe219267e5914a5d61399f3724057 to your computer and use it in GitHub Desktop.
Looking to get the timestamp from the next first day of the month based on today? Here you go
{% comment %} assign initial variables {% endcomment %}
{% comment %} nowts = now timestamp / nowd = day of the month / nowm = number of the month / sd = seconds of a single day {% endcomment %}
{% assign nowts = 'now' | date: '%s' %}
{% assign nowd = 'now' | date: '%e' %}
{% assign nowm = 'now' | date: '%m' %}
{% assign nowy = 'now' | date: '%Y' %}
{% assign sd = 86400 %}
{% comment %} finding out how many days left in the month {% endcomment %}
{% comment %} dim = days in month / drm = days remaining in month {% endcomment %}
{% comment %} we find how many days there are in the current month{% endcomment %}
{% comment %} we check if it's a month with 31 days{% endcomment %}
{% if nowm == '01' or nowm == '03' or nowm == '07' or nowm == '08' or nowm == '10' or nowm == '12' %}
{% assign dim = 31 %}
{% comment %} we check if it's February{% endcomment %}
{% elsif nowm == '02' %}
{% comment %} we check if it's a leap year{% endcomment %}
{% if nowy | modulo: 4 != 0 %}
{% assign dim = 29 %}
{% else %}
{% assign dim = 28 %}
{% endif %}
{% comment %} if it's none of those options, then it's a 30 days month{% endcomment %}
{% else %}
{% assign dim = 30 %}
{% endif %}
{% comment %} we calculate the number of days until the end of the month{% endcomment %}
{% assign drm = dim | minus: nowd %}
{% comment %} we add one to go over the next month{% endcomment %}
{% assign drm = drm | plus: 1 %}
{% comment %} we multiply the number of days to the next first by the number of seconds in a single day{% endcomment %}
{% assign drm = drm | times: sd %}
{% comment %} we add that result to the current timestamp{% endcomment %}
{% assign next_first = nowts | plus: drm %}
{{next_first}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment