Question

I am already using angular and moment js in a application and can't figured out how to use one or the other to fomat military time to regular am/pm. I can do it with a function or a filter but i bet there is a way with one of these two.

Thanks

Était-ce utile?

La solution

I would write my own filter, something like this:

//untested code!
.filter('milTimeToAMPM', function() {
    return function(time) {
        var returnString = time,
            hoursMin = (time.match(/^(\d{2})(\d{2})$/) || false);

        if(typeof time === 'string' &&
            hoursMin !== false) {
            returnString = (parseInt(time) < 1200) ?
                hoursMin[0] + ':' + hoursMin[1] + ' AM':
                (parseInt(hoursMin[0]) - 12) + ':' + hoursMin[1] + ' PM';
        }
        return returnString;
    }
})
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top