Created
March 12, 2025 00:07
-
-
Save NickDeckerDevs/ea2124d146117ce7e252edb708a1fa82 to your computer and use it in GitHub Desktop.
some fun date javascript functions and hubl macros
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# | |
This is my new favorite "Days Ago" but converted for partner portals where | |
action is needed and those "sales people" are out there hunting. They see | |
a "15 minutes" ago on a date property... whatever | |
#} | |
{% macro timeAgoSmart(date) %} | |
{%- set difference = local_dt|unixtimestamp - date|unixtimestamp -%} | |
{%- set minutes = (difference / 60000)|round(0, 'floor') -%} | |
{%- set hours = (difference / 3600000)|round(0, 'floor') -%} | |
{%- set days = (difference / 86400000)|round(0, 'floor') -%} | |
{%- set weeks = (days / 7)|round(0, 'floor') -%} | |
{%- set months = (days / 30.44)|round(0, 'floor') -%} | |
{%- set years = (days / 365.25)|round(1, 'floor') -%} | |
{%- if hours < 2 -%} | |
{{- minutes -}} minute{{ minutes > 1 ? 's' : '' }} ago | |
{%- elif days < 2 -%} | |
{{- hours -}} hour{{ hours > 1 ? 's' : '' }} ago | |
{% elif days >= 7 %} | |
{{- weeks -}} week{{ weeks > 1 ? 's' : '' }} ago | |
{%- elif days <= 90 -%} | |
{{- days -}} day{{ days > 1 ? 's' : '' }} ago | |
{%- elif months < 12 -%} | |
{{- months -}} month{{ months > 1 ? 's' : '' }} ago | |
{%- else -%} | |
{{- years -}} year{{ years > 1 ? 's' : '' }} ago | |
{%- endif -%} | |
{%- endmacro -%} | |
{# | |
date1: the first date (Newest Date) | |
date2: the second date (Oldest Date) | |
returns the difference in days between the two dates | |
#} | |
{%- macro differenceInDays(newDate, olderDate) -%} | |
{%- set difference = newDate|unixtimestamp - olderDate|unixtimestamp -%} | |
{{- (difference / 86400000)|round(0, 'floor') -}} | |
{%- endmacro -%} | |
{# | |
if you update below you can just return "X days ago" but I like to convert this later sometimes | |
{{- (difference / 86400000)|round(0, 'floor') -}} | |
to | |
{{- (difference / 86400000)|round(0, 'floor') ~ ' days ago' -}} | |
#} | |
{% macro daysAgo(date) %} | |
{%- set difference = local_dt|unixtimestamp - date|unixtimestamp -%} | |
{{- (difference / 86400000)|round(0, 'floor') -}} | |
{%- endmacro -%} | |
{# | |
can't remember why I made this, I'm not using it but it is here. If I recall correctly it does some | |
chaining witha date to get it to do somethign I needed it to do. Maybe ignore this | |
#} | |
{%- macro convertToDate(date) -%} | |
{%- set newDate = date|format_datetime("yyyy-MM-dd'T'HH:mm:ssZ")|strtotime("yyyy-MM-dd'T'HH:mm:ssZ") -%} | |
{{- newDate -}} | |
{%- endmacro -%} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
adjust the timings in this to better suit your client/organization. | |
I throw this in a serverless function to return dates that people can | |
read and take action on | |
*/ | |
function timeAgoSmart(inputTimestamp) { | |
if (!inputTimestamp) return 'better be a spider cuz you got a bug' | |
const now = Date.now() | |
const difference = now - inputTimestamp | |
const minutes = Math.floor(difference / 60000) | |
const hours = Math.floor(difference / 3600000) | |
const days = Math.floor(difference / 86400000) | |
const weeks = Math.floor(days / 7) | |
const months = Math.floor(days / 30.44) | |
const years = Math.floor(days / 365.25) | |
if (hours < 2) { | |
return `${minutes} minute${minutes > 1 ? 's' : ''} ago` | |
} else if (days < 2) { | |
return `${hours} hour${hours > 1 ? 's' : ''} ago` | |
} else if (days >= 7) { | |
return `${weeks} week${weeks > 1 ? 's' : ''} ago` | |
} else if (days <= 90) { | |
return `${days} day${days > 1 ? 's' : ''} ago` | |
} else if (months < 12) { | |
return `${months} month${months > 1 ? 's' : ''} ago` | |
} else { | |
return `${years} year${years > 1 ? 's' : ''} ago` | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment