-
-
Save ordenador/7d10f2c89421d4289361 to your computer and use it in GitHub Desktop.
fix parse date, and translate to spanish
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
.filter('timeago', function () { | |
/* | |
* time: the time | |
* local: compared to what time? default: now | |
* raw: wheter you want in a format of "5 minutes ago", or "5 minutes" | |
*/ | |
// parse string date to milliseconds | |
// Note: months are 0-based | |
function parseDate(input) { | |
var parts = input.split('-'); | |
var year = parts[0]; | |
var month = parts[1]; | |
var day = parts[2].split(':')[0].split('T')[0]; | |
var hours = parts[2].split(':')[0].split('T')[1]; | |
var minutes = parts[2].split(':')[1]; | |
var seconds = parts[2].split(':')[2].split('.')[0]; | |
var milliseconds = parts[2].split(':')[2].split('.')[1].split('Z')[0]; | |
return new Date(year, month-1, day, hours, minutes, seconds, milliseconds); | |
} | |
// get difference between UTC and local time in milliseconds | |
var timeZoneOffset = (new Date().getTimezoneOffset()) * 60000; | |
// filter ----------------------------------------------------------------------------- | |
return function (time, local, raw) { | |
if (!time) return "never"; | |
if (!local) { | |
(local = Date.now()); | |
} | |
if (angular.isDate(time)) { | |
time = time.getTime(); | |
} else if (typeof time === "string") { | |
time = parseDate(time); | |
} | |
// convert UTC to local | |
time = time - timeZoneOffset; | |
if (angular.isDate(local)) { | |
local = local.getTime(); | |
}else if (typeof local === "string") { | |
local = new Date(local).getTime(); | |
} | |
if (typeof time !== 'number' || typeof local !== 'number') { | |
return; | |
} | |
var span = [], | |
MINUTE = 60, | |
HOUR = 3600, | |
DAY = 86400, | |
WEEK = 604800, | |
MONTH = 2629744, | |
YEAR = 31556926, | |
DECADE = 315569260; | |
var offset = Math.abs((local - time) / 1000); | |
if (offset <= MINUTE) span = [ '', raw ? 'ahora' : 'menos de un minuto' ]; | |
else if (offset < (MINUTE * 60)) span = [ Math.round(Math.abs(offset / MINUTE)), 'minuto' ]; | |
else if (offset < (HOUR * 24)) span = [ Math.round(Math.abs(offset / HOUR)), 'hora' ]; | |
else if (offset < (DAY * 7)) span = [ Math.round(Math.abs(offset / DAY)), 'dia' ]; | |
else if (offset < (WEEK * 52)) span = [ Math.round(Math.abs(offset / WEEK)), 'semana' ]; | |
else if (offset < (YEAR * 10)) span = [ Math.round(Math.abs(offset / YEAR)), 'año' ]; | |
else if (offset < (DECADE * 100)) span = [ Math.round(Math.abs(offset / DECADE)), 'decada' ]; | |
else span = [ '', 'mucho mucho tiempo' ]; | |
span[1] += (span[0] === 0 || span[0] > 1) ? 's' : ''; | |
span = span.join(' '); | |
if (raw === true) { | |
return span; | |
} | |
return (time <= local) ? 'hace '+ span : 'dentro de ' + span; | |
}; | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment