Created
September 4, 2019 12:38
-
-
Save gabrielsaints/d6a06353e6e8c50c806e07e3abb70446 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const MONTH_NAMES = [ | |
'Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', | |
'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro', | |
]; | |
function getFormattedDate(date, prefomattedDate = false, hideYear = false) { | |
const day = date.getDate(); | |
const month = MONTH_NAMES[date.getMonth()]; | |
const year = date.getFullYear(); | |
const hours = date.getHours(); | |
let minutes = date.getMinutes(); | |
if (minutes < 10) { | |
// Adding leading zero to minutes | |
minutes = `0${minutes}`; | |
} | |
if (prefomattedDate) { | |
// Today at 10:20 | |
// Yesterday at 10:20 | |
return `${prefomattedDate} às ${hours}:${minutes}`; | |
} | |
if (hideYear) { | |
// 10. January at 10:20 | |
return `${day}. ${month} às ${hours}:${minutes}`; | |
} | |
// 10. January 2017. at 10:20 | |
return `${day}. ${month} ${year}. às ${hours}:${minutes}`; | |
} | |
// --- Main function | |
function timeAgo(dateParam) { | |
if (!dateParam) { | |
return null; | |
} | |
const date = typeof dateParam === 'object' ? dateParam : new Date(dateParam); | |
const DAY_IN_MS = 86400000; // 24 * 60 * 60 * 1000 | |
const today = new Date(); | |
const yesterday = new Date(today - DAY_IN_MS); | |
const seconds = Math.round((today - date) / 1000); | |
const minutes = Math.round(seconds / 60); | |
const isToday = today.toDateString() === date.toDateString(); | |
const isYesterday = yesterday.toDateString() === date.toDateString(); | |
const isThisYear = today.getFullYear() === date.getFullYear(); | |
if (seconds < 5) { | |
return 'agora'; | |
} if (seconds < 60) { | |
return `${seconds} segundos atrás`; | |
} if (seconds < 90) { | |
return 'cerca de um minuto atrás'; | |
} if (minutes < 60) { | |
return `${minutes} minutos atrás`; | |
} if (isToday) { | |
return getFormattedDate(date, 'Hoje'); // Today at 10:20 | |
} if (isYesterday) { | |
return getFormattedDate(date, 'Ontem'); // Yesterday at 10:20 | |
} if (isThisYear) { | |
return getFormattedDate(date, false, true); // 10. January at 10:20 | |
} | |
return getFormattedDate(date); // 10. January 2017. at 10:20 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment