mirror of https://github.com/wg-easy/wg-easy
committed by
Sergei Birukov
1 changed files with 24 additions and 24 deletions
@ -1,29 +1,29 @@ |
|||||
export function useTimeAgo() { |
export function useTimeAgo() { |
||||
const timeago = (date) => { |
const timeago = (date) => { |
||||
const now = new Date(); |
const now = new Date(); |
||||
const diff = now - date; |
const diff = now - date; |
||||
|
|
||||
if (diff < 1000) { |
if (diff < 1000) { |
||||
return "just now"; |
return 'just now'; |
||||
} else if (diff < 60000) { |
} else if (diff < 60000) { |
||||
const seconds = Math.floor(diff / 1000); |
const seconds = Math.floor(diff / 1000); |
||||
return `${seconds} ${plural("second", seconds)} ago`; |
return `${seconds} ${plural('second', seconds)} ago`; |
||||
} else if (diff < 3600000) { |
} else if (diff < 3600000) { |
||||
const minutes = Math.floor(diff / 60000); |
const minutes = Math.floor(diff / 60000); |
||||
return `${minutes} ${plural("minute", minutes)} ago`; |
return `${minutes} ${plural('minute', minutes)} ago`; |
||||
} else if (diff < 86400000) { |
} else if (diff < 86400000) { |
||||
const hours = Math.floor(diff / 3600000); |
const hours = Math.floor(diff / 3600000); |
||||
return `${hours} ${plural("hour", hours)} ago`; |
return `${hours} ${plural('hour', hours)} ago`; |
||||
} else { |
} else { |
||||
const days = Math.floor(diff / 86400000); |
const days = Math.floor(diff / 86400000); |
||||
return `${days} ${plural("day", days)} ago`; |
return `${days} ${plural('day', days)} ago`; |
||||
} |
} |
||||
}; |
}; |
||||
|
|
||||
// #278 -- this function might need to be rewritten to support l18n
|
// #278 -- this function might need to be rewritten to support l18n
|
||||
const plural = (word, count) => { |
const plural = (word, count) => { |
||||
return count !== 1 ? `${word}s` : word; |
return count !== 1 ? `${word}s` : word; |
||||
}; |
}; |
||||
|
|
||||
return { timeago }; |
return { timeago }; |
||||
} |
} |
||||
|
|||||
Loading…
Reference in new issue