Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Twitter like N min/sec ago timestamp in JS
- function timeSince(postDate) {
- var i, j, name, name2, seconds, seconds2, count, count2,
- chunks = [ // array of time period chunks
- [60 * 60 * 24 * 365, 'year'],
- [60 * 60 * 24 * 7, 'week'],
- [60 * 60 * 24, 'day'],
- [60 * 60, 'hour'],
- [1 * 60, 'minute'],
- [1 * 1, 'second']
- ],
- today = Math.round(new Date().getTime() / 1000),
- since = today - parseInt(postDate, 10);
- for (i = 0, j = chunks.length; i < j; i += 1) {
- seconds = chunks[i][0];
- name = chunks[i][1];
- if ((count = Math.floor(since / seconds)) != 0) { // finding the biggest chunk (if the chunk fits, break)
- break;
- }
- }
- print = (count == 1) ? '1 ' + name + ",": count + " " + name + "s";
- if (i + 1 < j - 1) { // now getting the second item
- seconds2 = chunks[i + 1][0];
- name2 = chunks[i + 1][1];
- if ((count2 = Math.floor((since - (seconds * count)) / seconds2)) != 0) {
- print += (count2 == 1) ? ', 1 ' + name2 : " " + count2 + " " + name2 + "s";
- }
- }
- return print;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement