Search code examples
angulartypescriptmomentjsdatetime-conversion

Converting military time to standard time using Moment.js


I'm getting an error when I try to use Moment.js to convert military time to standard 12 hour time. In my template I pass moment() an object that contains the time from a UI time picker, then I provide the desired format.

The terminal error says:

Parser Error: Unexpected token (, expected identifier or keyword at column 8 in [ [15:17:43] {{moment.(event.eventTime, hh:mm A)}}

<ion-col>
    <ion-icon name="clock"></ion-icon>
    <BR>
    {{ moment.(event.eventTime, hh:mm A) }}
</ion-col>

Solution

  • I suggest to use angular2-moment. You can use:

    • (if event.eventTime is a string) amParse pipe to parse your event.eventTime into a moment object (the pipe uses moment(String, String) parsing function). I'm supposing military time is in HH:mm format (e.g. 00:00, 15:30, etc)
    • amDateFormat pipe to display moment object in the desired format (hh:mm A)

    Your code could be like the following:

    {{ event.eventTime | amParse:'HH:mm' | amDateFormat:'hh:mm A' }}