amd-utils icon indicating copy to clipboard operation
amd-utils copied to clipboard

Date conversion to micro format

Open satazor opened this issue 13 years ago • 2 comments

It would be nice to have a function that receives a date object or a valid date string and converts it to a more human readable date:

  • 1 day ago
  • 1 week ago

Some libraries that implement this feature:

  • http://momentjs.com/
  • http://timeago.yarp.com/

satazor avatar May 03 '12 15:05 satazor

parsing date strings is tricky (many different formats) but converting milliseconds into the proper "scale" is very easy.

we should add an option to localize (second argument being an object with localization info), the localization scheme should support pluralization, thinking something like:

var ms = 1234567; // diff in milliseconds

ago(ms); // "20 minutes ago"

// l10n
var ptBr = {
  'minutes' : {
    '1' : '1 minuto atrás',
    'other' : '# minutos atrás'
  },
  'days' : {
    '1' : 'ontem', // yesterday
    '2' : 'antes de ontem', // so we can pass custom values for specific numbers
    'other' : '# dias atrás'
  },
  // ... weeks, months, years
};

ago(ms, ptBr); // "20 minutos atrás"

And if you pass a Date object it calculates the diff automatically.

var someDate = new Date(2011, 01, 01);
ago(someDate); // "1 year ago"

I think the utility to parse a date string into a Date object should be implemented separately and inside a date package. I think the ago() method should be inside the time package.

millermedeiros avatar May 03 '12 16:05 millermedeiros

Agree, but I think that having 2 separate packages for dealing with dates (date & time) could be confusing.

satazor avatar May 04 '12 17:05 satazor