Something I need to do from time to time is replicate the “days ago” feature that was built into BC.
This is what I’ve come up with:
{% assign nowSecond="now" | date: "%s" %}
{% assign daysAgo = this['ReleaseDate'] | date: "%s" | minus: nowSecond | divided_by: 3600 | divided_by: 24 | round | replace: '-', '' %}
{% assign monthsAgo = daysAgo | divided_by: 30.43 | round | replace: '-', '' %}
{% assign yearsAgo = monthsAgo | divided_by: 12 | round | replace: '-', '' %}
{% if monthsAgo >= 12 %}
{{yearsAgo}} year{% if yearsAgo > 1 %}s{% endif %}
{% elsif monthsAgo >= 1 %}
{{monthsAgo}} month{% if monthsAgo > 1 %}s{% endif %}
{% else %}
{{daysAgo}} day{% if daysAgo > 1 %}s{% endif %}
{% endif %}
ago
I’d like to get this added to the Treehouse code snippets as I thought it would be useful for others, but I thought perhaps somebody had a slicker way of doing this.
If you have a better way of doing this or suggestions, I’d appreciate the feedback.