self.calculateMessageTime = function calculateMessageTime(receiveDate){
var dateReceived = Date.parse(receiveDate);
var now = new Date();
now = Date.now();
// in seconds
var difference = Math.abs(dateReceived - now)/1000;
if(difference < 60) {
return "1 minute ago";
}else if(difference < (60*60) ) {
// minutes
return Math.floor(difference/60) + " minutes ago";
} else if(difference < (60*60*24)) {
// hours
var hoursPassed = Math.floor(difference/60/60) + " hours ago";
if(hoursPassed !== "1 hours ago") {
return Math.floor(difference/60/60) + " hours ago";
} else {
return Math.floor(difference/60/60) + " hour ago";
}
} else if(difference < (60*60*24*7)) {
// days
var daysPassed = Math.floor(difference/24/60/60) + " days ago";
if(daysPassed !== "1 days ago") {
return Math.floor(difference/24/60/60) + " days ago";
} else {
return Math.floor(difference/24/60/60) + " day ago";
}
} else if(difference < (60*60*24*7*4)) {
// weeks
var weeksPassed = Math.floor(difference/7/24/60/60) + " weeks ago";
if(weeksPassed !== "1 weeks ago") {
return Math.floor(difference/7/24/60/60) + " weeks ago";
} else {
return Math.floor(difference/7/24/60/60) + " week ago";
}
} else {
var monthsPassed = Math.floor(difference/4/7/24/60/60) + " months ago";
if(monthsPassed !== "1 months ago") {
return Math.floor(difference/4/7/24/60/60) + " months ago";
} else {
return Math.floor(difference/4/7/24/60/60) + " month ago";
}
}
};
The method above is designed to display a message indicating how long ago a notification was received on the UI. For example, "1 minute ago" or "1 day ago", and increment accordingly.
Currently, it correctly returns "1 minute ago," but at the exactly 60-second mark, I receive "1 minutes ago." This issue also occurs with day(s).
I suspect the problem may be due to comparing against 60 seconds when it should be 59? I'm quite puzzled by this. Any assistance would be greatly appreciated.