Angular directive/filter/service for formatting date so that it displays how long ago the given time was compared to now.
The format filter will only take effect if you've configured the service to display the full date after a certain number of seconds using the fullDateAfterSeconds setting. In this scenario, it will use the Angular date
filter with this format string.
{
secondsToMinute: 45,
secondsToMinutes: 90,
minutesToHour: 45,
minutesToHours: 90,
hoursToDay: 24,
hoursToDays: 42,
daysToMonth: 30,
daysToMonths: 45,
daysToYear: 365,
yearToYears: 1.5
}
This configure timeAgo
at which points changing the string format.
For example, the default behavior will display less than a minute ago
until 45 seconds, then it will display about a minute
.
If you want more languages: feel free to contribute!
The language is determined by the string in document.documentElement.lang
which you can set in your HTML markup:
<html lang="en_US"></html>
Or directly in JS:
window.document.documentElement.lang = 'en_US';
Or configure the service to override the default language:
angular.config(function (timeAgoSettings) {
timeAgoSettings.overrideLang = 'es_LA';
});
You can also add additional or alter existing languages at runtime by extending the service:
angular.config(function (timeAgoSettings) {
timeAgoSettings.strings.en_US = {
};
});
To add a new language if the one you want is missing :
- create a new file into the languages folder : src/languages/time-ago-language-xx_XX.js
- in that file, set your language definition as this :
'use strict';
angular.module('yaru22.angular-timeago').config(function(timeAgoSettings) {
timeAgoSettings.strings['xx_XX'] = {
... TODO =) ...
}
});
Language pickup
By adding many new language to the project, the library will get bigger and bigger overtime.
If you need only few languages, you may want no to embedded all the available languages.
So that, instead of linking to the full library like this :
<script src="bower_components/angular-timeago/dist/angular-timeago.min.js"></script>
you may prefer to pickup only the languages you want :
<script src="bower_components/angular-timeago/dist/angular-timeago-core.min.js"></script>
<script src="bower_components/angular-timeago/src/languages/time-ago-language-en_US.js"></script>
In that case, don't forget to concat your JS files into your own worklow.
Testing
In order to run the e2e tests you might need to install a Selenium server via:
./node_modules/grunt-protractor-runner/scripts/webdriver-manager-update
And then use grunt to run all tests (unit and e2e):