Created
May 1, 2014 07:33
-
-
Save richardkazuomiller/169ad18962e84c0814af to your computer and use it in GitHub Desktop.
Simple "ago" for Date
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
var lang = function(term){ | |
//todo other languages | |
return term; | |
} | |
Date.prototype.ago = function(){ | |
var now = new Date(); | |
var diff = (now - this)/1000; | |
if(diff < 60){ | |
return lang('Just now'); | |
} | |
else if(diff < 120){ | |
return [Math.floor(diff/60),lang('minute ago')].join(' '); | |
} | |
else if(diff < 3600){ | |
return [Math.floor(diff/60),lang('minutes ago')].join(' '); | |
} | |
else if(diff < 7200){ | |
return [Math.floor(diff/3600),lang('hour ago')].join(' '); | |
} | |
else if(diff < 86400){ | |
return [Math.floor(diff/3600),lang('hours ago')].join(' '); | |
} | |
else if(diff < 86400*2){ | |
return lang('Yesterday'); | |
} | |
else if(diff < 86400*4){ | |
return [Math.floor(diff/86400),lang('days ago')].join(' '); | |
} | |
return this.toString('yyyy/MM/dd HH:mm'); | |
} | |
})(); | |
var testdate = new Date(new Date().getTime()-1000); | |
console.log(testdate.ago()); | |
var testdate = new Date(new Date().getTime()-60000); | |
console.log(testdate.ago()); | |
var testdate = new Date(new Date().getTime()-3600000); | |
console.log(testdate.ago()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment