Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function xTimeAgo(time) {
- var nd = new Date();
- //var gmtDate = Date.UTC(nd.getFullYear(), nd.getMonth(), nd.getDate(), nd.getHours(), nd.getMinutes(), nd.getMilliseconds());
- var gmtDate = Date.parse(nd);
- var tweetedTime = Date.parse(time);
- var timeDiff = (gmtDate - tweetedTime) / 1000;
- //convert milliseconds to seconds
- var second = 1, minute = 60, hour = 60 * 60, day = 60 * 60 * 24, week = 60 * 60 * 24 * 7, month = 60 * 60 * 24 * 30, year = 60 * 60 * 24 * 365;
- if (timeDiff > second && timeDiff < minute) {
- return Math.round(timeDiff / second) + " second"+oneOreMany(Math.round(timeDiff / second))+" ago";
- } else if (timeDiff >= minute && timeDiff < hour) {
- return Math.round(timeDiff / minute) + " minute"+oneOreMany(Math.round(timeDiff / minute))+" ago";
- } else if (timeDiff >= hour && timeDiff < day) {
- return Math.round(timeDiff / hour) + " hour"+oneOreMany(Math.round(timeDiff / hour))+" ago";
- } else if (timeDiff >= day && timeDiff < week) {
- return Math.round(timeDiff / day) + " day"+oneOreMany(Math.round(timeDiff / day))+" ago";
- } else if (timeDiff >= week && timeDiff < month) {
- return Math.round(timeDiff / week) + " week"+oneOreMany(Math.round(timeDiff / week))+" ago";
- } else if (timeDiff >= month && timeDiff < year) {
- return Math.round(timeDiff / month) + " month"+oneOreMany(Math.round(timeDiff / month))+" ago";
- } else {
- return 'over a year ago';
- }
- }
- function oneOreMany (arg) {
- if (arg == 1) {
- return '';
- } else{
- return 's';
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement