Skip to content

Instantly share code, notes, and snippets.

@Kolesias123
Created December 22, 2014 16:48
Show Gist options
  • Save Kolesias123/77be7b1bddb423d962b6 to your computer and use it in GitHub Desktop.
Save Kolesias123/77be7b1bddb423d962b6 to your computer and use it in GitHub Desktop.
function calculate(time, num)
{
var inte = ["segundo", "minuto", "hora", "día", "semana", "mes", "año"];
var dur = [60, 60, 24, 7, 4.35, 12];
time = parseInt(time);
var now = parseInt( Math.round((new Date()).getTime() / 1000) );
var j = 0;
var dif = 0;
var str = "";
var sh = time + 10;
//if(now == time || now < sh)
// return "Justo ahora";
if(now > time)
{
dif = now - time;
str = "Hace";
}
else
{
dif = time - now;
str = "Dentro de";
}
for(j = 0; dif >= dur[j] && j < dur.length - 1; j++)
dif /= dur[j];
dif = Math.round(dif);
if(dif != 1)
{
inte[5] += "e";
inte[j] += "s";
}
if(num == true)
return dif + ' ' + inte[j];
else
return str + " " + dif + " " + inte[j];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment