user474901
user474901

Reputation:

angular filter is date time format-er

I have angular Filter and always show wrong time from json is '2015-04-09T16:30:00' the problem is it show time 2015-04-09 12:30 PM off 4 hour of correct time how can I show corect time ?

app.filter('formatDateAndTime', function () {
    return function (input){
        if (moment.utc(input).local().format('MM/DD/YYYY hh:mm ') === 'Invalid date')
            return ' ';
        else
            return moment.utc(input).local().format('MM/DD/YYYY hh:mm ');
    };
});

Upvotes: 0

Views: 187

Answers (2)

David Balažic
David Balažic

Reputation: 1474

The moment.utc(x) call interprets the input as UTC time and then .local() outputs it in local timezone, which is 4 hours off.

Oops, I missed the part of the question about getting the same time. See Ben Whitney's answer (use moment(input) instead of moment.utc(input))

Upvotes: 1

Ben Whitney
Ben Whitney

Reputation: 29

As David mentions you don't want to use UTC.

Use moment(x) instead of moment.utc(x).

So your code would be:

app.filter('formatDateAndTime', function () {
    return function (input){
        if (moment(input).local().format('MM/DD/YYYY hh:mm ') === 'Invalid date')
        return ' ';
        else
            return moment(input).local().format('MM/DD/YYYY hh:mm ');
    };
});

Upvotes: 1

Related Questions