Skip to content

Instantly share code, notes, and snippets.

@roobottom
Last active September 8, 2015 09:32
Show Gist options
  • Save roobottom/841fd91bb7b03eaa1948 to your computer and use it in GitHub Desktop.
Save roobottom/841fd91bb7b03eaa1948 to your computer and use it in GitHub Desktop.
A fuzzy date calculator for Jekyll.
{% if include.daysDiff >= 365 %}
{% assign yearDiff = include.daysDiff | divided_by: 365 | round %}
{% assign yearRemainder = include.daysDiff | modulo: 365 | divided_by: 30 | round %}
{% if yearRemainder != 0 %}
{% if yearRemainder > 10 %}
Almost {{ yearDiff + 1 }} year{% if yearDiff > 1 %}s{% endif %} {{ include.label }}
{% elsif yearRemainder >= 6 %}
{% if yearDiff == 1 %}
A year and a half {{ include.label }}
{% else %}
{{ yearDiff }} and a half years {{ include.label }}
{% endif %}
{% else %}
{% if yearDiff == 1 %}
Just over a year {{ include.label }}
{% else %}
Just over {{ yearDiff }} years {{ include.label }}
{% endif %}
{% endif %}
{% else %}
{{ yearDiff }} year{% if yearDiff > 1 %}s{% endif %} {{ include.label }}
{% endif %}
{% elsif include.daysDiff >= 30 %}
{% assign monthDiff = include.daysDiff | divided_by: 30 | round %}
{% if monthDiff > 1 %}
{{ monthDiff }} months {{ include.label }}
{% else %}
A month {{ include.label }}
{% endif %}
{% elsif include.daysDiff >= 7 %}
{% assign weekDiff = include.daysDiff | divided_by: 7 | round %}
{% if weekDiff > 1 %}
{{ weekDiff }} weeks {{ include.label }}
{% else %}
A week {{ include.label }}
{% endif %}
{% elsif include.daysDiff > 1 %}
{{ include.daysDiff }} days {{ include.label }}
{% elsif include.daysDiff == 1 %}
A day {{ include.label }}
{% else %}
On the same day
{% endif %}
{% for post in site.posts %}
{% assign prevDate = post.prev.date | date: '%s' %}
{% assign thisDate = post.date | date: '%s' %}
{% assign prevDateDiff = thisDate | minus: prevDate | divided_by: 86400 %}
{% if forloop.index != 1 %}
This post was written {% include fuzzy-date.html dayDiff=prevDateDiff label='after' %} the last.
{% endif %}
{% endfor %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment