amd-utils
amd-utils copied to clipboard
Date conversion to micro format
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/
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.
Agree, but I think that having 2 separate packages for dealing with dates (date & time) could be confusing.