TidharPeer
TidharPeer

Reputation: 3797

Angular-Translate with custom filter

How can I use my own custom filter to format lets say, day in week to be not Sunday, Monday... but to use my $translate service. For Example

app.filter('formatDate', ['$translate', function ($translate) {
return function (date) {
    var now = new Date();
    var date = new Date(date);
    var monthNames = [
        "Jan", "Feb", "March",
        "Apr", "May", "June", "July",
        "Aug", "Sept", "Oct",
        "Nov", "Dec"
    ];
    dayNames = [
    "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
    ];

    var formattedDate;
    ...
    ...

    if (now.getDate() == date.getDate() && now.getMonth() == date.getMonth() && now.getFullYear() == date.getFullYear()) {
        if (diffSeconds < 120) {
            formattedDate = 'few seconds ago';
        }
        else {
            if (diffSeconds <= 300) {
                formattedDate = Math.round(diffSeconds / 60) + ' minutes ago';
            }
            else {
                formattedDate ='at '+hours + ':' + minutes + ampm;
            }
        }
    }
    else {
        if(diffDays == 1)
            formattedDate = 'yesterday';
        else {
            if (diffDays < 7){
                formattedDate = 'on ' + dayNames[date.getDay()];
            }
            else{
                formattedDate = monthNames[date.getMonth()] + ' ' + date.getDate() + ', ' + date.getFullYear();
                }
            }
        }

    return formattedDate;
};
}]);

How can I have the array dayNames to use my translated days in weeks?

Upvotes: 0

Views: 496

Answers (1)

TidharPeer
TidharPeer

Reputation: 3797

I used $translate.instant() to solve this...

dayNames = [
    $translate.instant("Sunday"), 
    $translate.instant("Monday"), 
    $translate.instant("Tuesday"), 
    $translate.instant("Wednesday"), 
    $translate.instant("Thursday"), 
    $translate.instant("Friday"), 
    $translate.instant("Saturday")
];

Upvotes: 1

Related Questions