dateFormat.js 957 B

123456789101112131415161718192021222324252627282930
  1. export const dateFormat = (item) => {
  2. const lang = navigator.language;
  3. const rtf = new Intl.RelativeTimeFormat( lang, { numeric: 'auto' });
  4. const dateSecDelta = Math.round((new Date(item.createDate).getTime() - Date.now())/1000) ;
  5. const secondTimeMarkers = [60, 3600, 86400, 86400 * 7, 86400 * 30, 86400 * 365, Infinity];
  6. const literalTimeMarkers = ['second','minute', 'hour', 'day', 'week', 'month' ,'year'];
  7. const indexTime = secondTimeMarkers.findIndex(time => time > Math.abs(dateSecDelta))
  8. const divisor = indexTime ? secondTimeMarkers[indexTime - 1] : 1 //for seconds if index 0 then seconds
  9. const test = rtf.format(Math.floor(dateSecDelta/divisor),literalTimeMarkers[indexTime])
  10. // //need to change on Moment js
  11. // const res = item.createDate.split('T');
  12. // const date = {
  13. // year : res[0],
  14. // time : res[1].slice(-13, -5)
  15. // }
  16. return test;
  17. }
  18. //date formating