Filippo oretti
Filippo oretti

Reputation: 49817

Angular js - formatting datetime to match condition

Hi i'm simply printing out datetime using the Angular date filter and this is how my code looks actually:

<small class="muted smaller">

        {{row.insert_datetime |  date : 'shortTime'}},
        {{row.insert_datetime | date : 'longDate'}}
    </small>

so the result is pretty good and it returns :

<small class="muted smaller">
            5:22 PM, 1 April 2010
</small>

What i would like more is to return something like this with conditions:

if(today == 1 April 2010){
print 5:22 PM
}else{
print 5:22 PM , 1 April 2010
}

Is it possible in some way to add this condition ?

Upvotes: 1

Views: 765

Answers (2)

Prashobh
Prashobh

Reputation: 9542

You can use angular filter concept , you can modify this to further to achieve your goal

angular.module('yourmodule').filter('hbdate', function($filter)
{
    return function(input)
    {
        if(input == null){ return ""; }
        var _date = $filter('date')(new Date(input), 'MMM dd yyyy');
                if{
                 your condition 
                 }

        return _date.toUpperCase();
    };
});

Date filtering and formatting in Angular js.

Upvotes: 1

Nikos Paraskevopoulos
Nikos Paraskevopoulos

Reputation: 40296

It is doable as:

<span>
    {{ row.insert_datetime |  date : 'shortTime' }}
    {{ row.insert_datetime === today && ' ' || (',' + (row.insert_datetime | date : 'longDate')) }}
</span>

But it is ugly; maybe you should try doing it in code. (If you do it, note the space in the string after the && - it is needed.)

Upvotes: 1

Related Questions