Last active
May 1, 2017 23:40
-
-
Save SimonLlewellyn/9c3155414368c134655931c28bd4a9ea to your computer and use it in GitHub Desktop.
CF Function - return how long ago a date/time was: timeAgoFormat(date).short
This file contains hidden or 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
<cfscript> | |
function timeAgoFormat(date){ | |
var interval = ""; | |
var offset = 0; | |
var result = 0; | |
var res = structNew(); | |
if (isDate(arguments.date)){ | |
var formattedDate = dateFormat(arguments.date, "ddd dd mmm yy"); | |
//& " at " & timeFormat(arguments.date, "HH:MM"); | |
if (dateDiff("s", arguments.date, now()) < 60){ | |
// less than 1 minute show interval in seconds | |
offset = dateDiff("s", arguments.date, now()); | |
interval= offset == 1 ? "second":"secs"; | |
if(offset < 1) offset = 0; | |
//result = "#offset# #interval# ago"; | |
result = "Just Now"; | |
}else if (dateDiff("n", arguments.date, now()) < 60){ | |
// less than 1 hour show interval in minutes | |
offset = dateDiff("n", arguments.date, now()); | |
interval= offset == 1 ? "min":"mins"; | |
result = "#offset# #interval# ago"; | |
}else if (dateDiff("h", arguments.date, now()) < 24){ | |
// less than 24 hours display interval in hours | |
offset = dateDiff("h", arguments.date, now()); | |
interval= offset == 1 ? "hr":"hrs"; | |
result = "#offset# #interval# ago"; | |
}else if (dateDiff("d", arguments.date, now()) < 2){ | |
// less than 2 days display yesterday | |
result = "Yesterday at " & lcase(timeFormat(arguments.date,'h:mm:sstt')); | |
}else if (dateDiff("d", arguments.date, now()) < 7){ | |
// less than 7 days display day | |
//result = dayOfWeekAsString( dayOfWeek( arguments.date )); | |
offset = dateDiff("d", arguments.date, now()); | |
interval= offset == 1 ? "day":"days"; | |
result = "#offset# #interval# ago";<!--- & lcase(timeFormat(arguments.date,'h:mmtt'));---> | |
}else if (dateDiff("d", arguments.date, now()) > 7){ | |
result = formattedDate | |
}else{ | |
// otherwise display date | |
result = formattedDate; | |
} | |
// interval = "<abbr title='" & formattedDate & "'>" & result & "</abbr>"; | |
res.full = formattedDate; | |
res.short = result; | |
res.long = formattedDate & " " & lcase(timeFormat(arguments.date,'h:mmtt')); | |
} | |
return res; | |
} | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment