Skip to content

Instantly share code, notes, and snippets.

@avinashgardas
Last active March 26, 2018 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avinashgardas/dc0cbf12d97907246a3b11cb9966abaa to your computer and use it in GitHub Desktop.
Save avinashgardas/dc0cbf12d97907246a3b11cb9966abaa to your computer and use it in GitHub Desktop.
Time ago function in javascript (example: 3 days ago)
function console(msg) {
var pre = document.createElement('pre')
pre.innerHTML = msg
document.body.appendChild(pre)
}
function getFeedbackTime(feedbackTimestamp) {
/*
Note: timestamp structure
2018 - 03 - 26 T 1 4 : 2 7 : 4 8 . 5 0 8 Z
01234 567 8910 111213 141516 171819 202122 2324
*/
//compare timestamps only when length equals 24
if(feedbackTimestamp.length === 24) {
//variables
let currentTimestamp = new Date().toISOString()
const feedback_year = feedbackTimestamp.substring(0,4)
const current_year = currentTimestamp.substring(0,4)
const feedback_month = feedbackTimestamp.substring(5,7)
const current_month = currentTimestamp.substring(5,7)
const feedback_day = feedbackTimestamp.substring(8,10)
const current_day = currentTimestamp.substring(8,10)
const feedback_hour = feedbackTimestamp.substring(11,13)
const current_hour = currentTimestamp.substring(11,13)
const feedback_minute = feedbackTimestamp.substring(14,16)
const current_minute = currentTimestamp.substring(14,16)
const feedback_second = feedbackTimestamp.substring(17,19)
const current_second = currentTimestamp.substring(17,19)
console(currentTimestamp)
console(feedbackTimestamp)
//years
if(current_year !== feedback_year) {
const current_year_number = parseInt(current_year)
const feedback_year_number = parseInt(feedback_year)
const year_diff = current_year_number-feedback_year_number
if(year_diff > 1) {
console(year_diff+' years ago')
} else if(year_diff < 0) {
console(year_diff+' year(s) ahead : invalid scenario')
} else {
console(year_diff+' year ago')
}
} else if(current_month !== feedback_month){
//months
const current_month_number = parseInt(current_month)
const feedback_month_number = parseInt(feedback_month)
const month_diff = current_month_number - feedback_month_number
if(month_diff > 1) {
console(month_diff+' months ago')
} else if(month_diff < 0) {
console(month_diff+' month(s) ahead : invalid scenario')
} else {
console(month_diff+' month ago')
}
} else if(current_day !== feedback_day) {
//days
const current_day_number = parseInt(current_day)
const feedback_day_number = parseInt(feedback_day)
const day_diff = current_day_number - feedback_day_number
if(day_diff > 1) {
console(day_diff+' days ago')
} else if(day_diff < 0) {
console(day_diff+' day(s) ago : invalid scenario')
} else {
console(day_diff+' day ago')
}
} else if(current_hour !== feedback_hour) {
//hours
const current_hour_number = parseInt(current_hour)
const feedback_hour_number = parseInt(feedback_hour)
const hour_diff = current_hour_number - feedback_hour_number
if(hour_diff > 1) {
console(hour_diff+' hours ago')
} else if(hour_diff < 0) {
console(hour_diff+' hour(s) ahead : invalid scenario')
} else {
console(hour_diff+' hour ago')
}
} else if(current_minute !== feedback_minute) {
//minutes
const current_minute_number = parseInt(current_minute)
const feedback_minute_number = parseInt(feedback_minute)
const minute_diff = current_minute_number - feedback_minute_number
if(minute_diff > 1) {
console(minute_diff+' minutes ago')
} else if(minute_diff < 0) {
console(minute_diff+' minute(s) ahead : invalid scenario')
} else {
console(minute_diff+' minute ago')
}
} else if(current_second !== feedback_second) {
//seconds
const current_second_number = parseInt(current_second)
const feedback_second_number = parseInt(feedback_second)
const second_diff = current_second_number - feedback_second_number
if(second_diff > 1) {
console(second_diff+' seconds ago')
} else if(second_diff < 0) {
console(second_diff+' second(s) ahead : invalid scenario')
} else {
console(second_diff+' second ago')
}
} else if(current_year === feedback_year) {
console('just now')
}
}
else {
console('timestamps can\'t be compared')
}
}
getFeedbackTime('2018-03-26T16:09:45.291Z')
@avinashgardas
Copy link
Author

Add timeAgo.js to an html file (<script src='timeAgo.js'></script>) and watch the output.

@avinashgardas
Copy link
Author

Output format shall look like

  • 2 years ago
  • 1 month ago
  • 5 days ago
  • 3 hours
  • 30 minutes ago
  • 15 seconds ago

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment