Last active
December 28, 2015 05:09
-
-
Save Frogmouth/7448280 to your computer and use it in GitHub Desktop.
A simple function to calcunate how much time has elapsed since a date, and return a text template.
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
/*sinceHow | |
@Author Simone Luise | |
@Mail me@simoneluise.com | |
A simple function to calcunate how much time has elapsed since a date. | |
The funcion have only one attribute: "time". | |
Time must be a valid string for generate a new Data() object. Like: | |
. "2013-11-08T09:23:35.000Z" - formatted date | |
. 1383902615000 - millisecond date | |
. ecc... | |
*/ | |
var sinceHow = function (time) { | |
var tmpl, div; | |
var diff = new Date() - new Date(time); | |
/* *1 yeaer, mouth, day, hour, minute, in millisecond (you can add other time step, but go to commento *2) */ | |
var tMilli = [31104000000, 2592000000, 86400000, 3600000, 60000, 0]; | |
/* *2 array of array with function response text, the array index is relatives to the same index in tMilli array, each item is an array because you can use singolar or plural, {{NUMBER}} is a placeholder that will be replace for number*/ | |
var tplMilli = [ | |
["a year ago", "{{NUMERO}} years ago"], | |
["About a month ago", "{{NUMERO}} About a month ago"], | |
["About a day ago", "{{NUMERO}} days ago"], | |
["About an hour ago", "{{NUMERO}} hours ago"], | |
["About a minute ago", "{{NUMERO}} minutes ago"], | |
["In this moment", "A few seconds ago"] | |
]; | |
/* Function for generate and populate the template */ | |
var basicTmpl = function (data, tmpl) { | |
var regexp; | |
for (placeholder in data) { | |
regexp = new RegExp('{{' + placeholder + '}}', 'g'); | |
tmpl = tmpl.replace(regexp, data[placeholder]); | |
} | |
return tmpl; | |
} | |
for (var k in tMilli) { | |
if (diff >= tMilli[k]) { | |
div = diff / tMilli[k]; | |
tmpl = (div < 2) ? tplMilli[k][0] : tplMilli[k][1]; | |
return this.basicTmpl({ | |
NUMERO: parseInt(div) | |
}, tmpl); | |
} | |
} | |
} |
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
/* @Author Simone Luise - me@simoneluise.com */ | |
var sinceHow=function(e){var t,n;var r=new Date-new Date(e);var i=[31104000000, 2592000000, 86400000, 3600000, 60000, 0];var s=[["a year ago","{{NUMERO}} years ago"],["About a month ago","{{NUMERO}} About a month ago"],["About a day ago","{{NUMERO}} days ago"],["About an hour ago","{{NUMERO}} hours ago"],["About a minute ago","{{NUMERO}} minutes ago"],["In this moment","A few seconds ago"]];var o=function(e,t){var n;for(placeholder in e){n=new RegExp("{{"+placeholder+"}}","g");t=t.replace(n,e[placeholder])}return t};for(var u in i){if(r>=i[u]){n=r/i[u];t=n<2?s[u][0]:s[u][1];return this.basicTmpl({NUMERO:parseInt(n)},t)}}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
min verstion is not tested