I'm making a javascript counter that counts 'seconds ago'. I have my time in a JS time object, and I found a "time difference" function snippet here on stack overflow, but it displays "2 hours ago". How can I get it to display "5 hours, 10 minutes and 37 seconds ago."
Here's what I'm working with:
This function converts the current time and the timestamp of something into "20 seconds ago" instead of a cryptic date:
function timeDifference(current, previous) {
var msPerMinute = 60 * 1000;
var msPerHour = msPerMinute * 60;
var msPerDay = msPerHour * 24;
var msPerMonth = msPerDay * 30;
var msPerYear = msPerDay * 365;
var elapsed = current - previous;
if (elapsed < msPerMinute) {
return Math.round(elapsed/1000) + ' seconds ago';
} else if (elapsed < msPerHour) {
return Math.round(elapsed/msPerMinute) + ' minutes ago';
} else if (elapsed < msPerDay ) {
return Math.round(elapsed/msPerHour ) + ' hours ago';
} else if (elapsed < msPerMonth) {
return 'approximately ' + Math.round(elapsed/msPerDay) + ' days ago';
} else if (elapsed < msPerYear) {
return 'approximately ' + Math.round(elapsed/msPerMonth) + ' months ago';
} else {
return 'approximately ' + Math.round(elapsed/msPerYear ) + ' years ago';
}
}
And here's what I'm using to "count up" the time each second. I'd like it to say "5 hours, 3 minutes, 10 seconds ago" and then 1 second later, "5 hours, 3 minutes, 11 seconds ago"
var newTime = new Date(data.popular[i].timestamp*1000)
var relTime = timeDifference(new Date(),newTime)
setInterval(function(){
var theTimeEl = $('.timestamp-large').filter(function(){
return $(this).html() == relTime
});
newTime.setSeconds(newTime.getSeconds() + 1);
var relTime = timeDifference(new Date(), newTime);
$(theTimeEl).html(relTime);
console.log(relTime)
}, 1000)
The variable newTime is the time in the UTC javascript date format. relTime is that in "seconds ago" format. The interval loops through a bunch of timestamp elements and picks the right one for each time stamp. Then it adds a second to the time, converts it back into "fuzzy time" (seconds ago), replaces the html with the new time and logs it in the console.
How do I change "5 hours ago" to "5 hours, 37 mintues, 10 seconds ago"? The time difference function needs to be modified.
Here's a function that is close to what you're asking for.
var timeparts = [
{name: 'year', div: 31536000000, mod: 10000},
{name: 'day', div: 86400000, mod: 365},
{name: 'hour', div: 3600000, mod: 24},
{name: 'minute', div: 60000, mod: 60},
{name: 'second', div: 1000, mod: 60}
];
function timeAgoNaive(comparisonDate) {
var
i = 0,
l = timeparts.length,
calc,
values = [],
interval = new Date().getTime() - comparisonDate.getTime();
while (i < l) {
calc = Math.floor(interval / timeparts[i].div) % timeparts[i].mod;
if (calc) {
values.push(calc + ' ' + timeparts[i].name + (calc != 1 ? 's' : ''));
}
i += 1;
}
if (values.length === 0) { values.push('0 seconds'); }
return values.join(', ') + ' ago';
}
console.log(timeAgoNaive(new Date(Date.parse('Jun 12 2006 11:52:33'))));
console.log(timeAgoNaive(new Date(new Date().getTime() - 3600000)));
console.log(timeAgoNaive(new Date()));
Results:
6 years, 33 days, 4 hours, 52 minutes, 22 seconds ago
1 hour ago
0 seconds ago
I called it "naive" because it doesn't really pay attention to the human way that we calculate time. If it is "1/1/2013 12:01:00 am" exactly, comparing to "1/1/2012 12:01:00 am" should yield "1 year, 0 months, 0 days, 0 hours, 0 minutes, 0 seconds ago". But it won't do that by extending the logic in the function you presented, and it won't do that in my function either (plus my function won't use months). A better approximation of years than 365 days is 365.24, but that also is ignored.
I excluded the empty time parts as you requested, leaving "0 seconds" at a minimum when there are no time parts found.
Now, if you want that human-like way of calculating, you have to decide some things. You can't just use boundaries crossed because Feb 28 to Mar 1 is not a whole month. Second, here's a question will expose the real problem:
If you calculate Feb 2 to Mar 2 as one month, then it's 1 month 29 days. But what if it were Jan 2 to Mar 1? That's the same number of days elapsed between them. Is that now 1 month (for all of April) + 1 day in March + the 31 days in Jan for 1 month 32 days? Do you want your months to coincide to a physical calendar so a human could back track with his finger and get the correct date out of it? That is much harder than you think.
If you can answer with sensible and complete rules about how you would do "human-like elapsed time figuring" then maybe I can write you another function to do it.
Update
Here's a new function that does months, and has 365.24 days in a year (30.43666666 days in a month):
var timeparts = [
{name: 'millenni', div: 31556736000, p: 'a', s: 'um'},
{name: 'centur', div: 3155673600, p: 'ies', s: 'y'},
{name: 'decade', div: 315567360},
{name: 'year', div: 31556736},
{name: 'month', div: 2629728},
{name: 'day', div: 86400},
{name: 'hour', div: 3600},
{name: 'minute', div: 60},
{name: 'second', div: 1}
];
function timeAgoNaive2(comparisonDate) {
var i = 0,
parts = [],
interval = Math.floor((new Date().getTime() - comparisonDate.getTime()) / 1000);
for ( ; interval > 0; i += 1) {
var value = Math.floor(interval / timeparts[i].div);
interval = interval - (value * timeparts[i].div);
if (value) {
parts.push(value + ' ' + timeparts[i].name + (value !== 1 ? timeparts[i].p || 's' : timeparts[i].s || ''));
}
}
if (parts.length === 0) { return 'now'; }
return parts.join(', ') + ' ago';
}
console.log(timeAgoNaive2(new Date(Date.parse('Jun 12 2006 11:52:33'))));
console.log(timeAgoNaive2(new Date(new Date().getTime() - 3600000)));
console.log(timeAgoNaive2(new Date()));
console.log(timeAgoNaive2(new Date(-92709631247000)));
Output:
6 years, 1 month, 1 day, 10 hours, 53 minutes, 44 seconds ago
1 hour ago
now
2 millennia, 9 centuries, 8 decades, 4 months, 26 days, 22 hours, 41 minutes, 47 seconds ago
It is still naive, but it does a little better job. Plus it will work for REALLY old dates like B.C. ones. :)
Change the logic so that rather than just finding the single greatest unit of measurement it can, it does something with the remainder.
Basically what you'd need to do is start with the greatest increment, find the value, then subtract it from the total to get the remainder. Then repeat.
Something like this maybe, I haven't tested it.
var elapsed = current - previous;
var remainder = elapsed;
int years;
int months;
years = Math.floor(remainder/msPerYear);
remainder = remainder % msPerYear;
months = Math.floor(remainder/msPerMonth);
remainder = remainder % msPerMonth;
// repeat
Then just build your string off the variables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With