Hey everyone, so I'm wanting to take a date from string (e.g. "2023-12-04T01:05:21.140365+00:00") and output a relative time. Such as "3 weeks ago" or "2 days ago" or "3 hours ago", depending on how long in the past it was. Then outputting it to a variable in my component. Something similar to the below. I've tried creating a custom formula but just can't get it working (custom code is still pretty fresh to me so I'm probably doing something silly). function timeAgo(dateString) { const date = new Date(dateString).getTime(); const now = new Date().getTime(); const diffMs = now - date; const diffSecs = Math.floor(diffMs / 1000); const diffMins = Math.floor(diffSecs / 60); const diffHours = Math.floor(diffMins / 60); const diffDays = Math.floor(diffHours / 24);
if (diffDays > 0) { return ${diffDays} days ago; } else if (diffHours > 0) { return ${diffHours} hours ago; } else if (diffMins > 0) { return ${diffMins} minutes ago; } else { return ${diffSecs} seconds ago; } } Any ideas?
Lucas G
2 years ago
Looking at it, the literals wouldn't work
They need the backticks
Stockton
2 years ago
AI wrote this, haven’t tried it yet. function timeAgo(timestamp){ var currentDate = new Date(); var inputDate = new Date(timestamp); var diffSec = Math.floor((currentDate - inputDate) / 1000); if (diffSec < 0) return 'in the future'; if (diffSec < 60) return 'just now'; var diffMin = Math.floor(diffSec/60); if (diffMin < 60) return diffMin + ' minutes ago'; var diffHrs = Math.floor(diffSec/3600); if (diffHrs < 24) return diffHrs + ' hours ago'; var diffDays = Math.floor(diffSec/86400); if (diffDays < 30) return diffDays + ' days ago'; var diffMons = Math.floor(diffSec/2592000); if (diffMons < 12) return diffMons + ' months ago'; var diffYears = Math.floor(diffSec/31104000); return diffYears + ' years ago'; } console.log(timeAgo('2023-12-04T01:05:21.140365+00:00')); // test
Lucas G
2 years ago
@Tom Wrench add the backticks for the literals and tweak the argument
function timeAgo(dateString) { const date = new Date(args.dateString).getTime(); const now = new Date().getTime(); const diffMs = now - date; const diffSecs = Math.floor(diffMs / 1000); const diffMins = Math.floor(diffSecs / 60); const diffHours = Math.floor(diffMins / 60); const diffDays = Math.floor(diffHours / 24); if (diffDays > 0) { return `${diffDays} days ago`; } else if (diffHours > 0) { return `${diffHours} hours ago`; } else if (diffMins > 0) { return `${diffMins} minutes ago`; } else { return `${diffSecs} seconds ago`; } }
Oh I see, Discord removes the literals because of the code markdown haha
Awesome, thank you. I'm probably being a numpty, but how the heck were you able to expose it in your formula panel? It simply doesn't exist for me... And yes, months and years would be a sensible inclusion too 🙂
It's part of the reason why I made that other dateFilter formula I shared a while back
Tom Wrench
2 years ago
Of course! As @Tom Ireland would say, every day's a school day. Thanks Lucas - got it working 🙏
🔥1
Lucas G
2 years ago
It is indeed
I will probably save this formula too so thank you for sharing it lol
Tom Wrench
2 years ago
might add in the weeks/months/years to finalise
Lucas G
2 years ago
Great
Please share it if you do
Tom Wrench
2 years ago
Added weeks/months/years, plus also adding the correct singular/plural logic for the units (so e.g. it would be "1 week" rather than "1 weeks". function timeAgo(dateString) { const date = new Date(args.dateString).getTime(); const now = new Date().getTime(); const diffMs = now - date; const diffSecs = Math.floor(diffMs / 1000); const diffMins = Math.floor(diffSecs / 60); const diffHours = Math.floor(diffMins / 60); const diffDays = Math.floor(diffHours / 24); const diffWeeks = Math.floor(diffDays / 7); const diffMonths = Math.floor(diffDays / 30); // Approximation const diffYears = Math.floor(diffDays / 365); // Approximation