ionMobDev
ionMobDev

Reputation: 361

convert string to Time format in typescript

I have to convert my server data in string format as 13:47 to 01:47PM but am trying with {{time | date:"hh:MM"}} and {{task.time | date: 'shortTime'}} but it showing an datepipe error and arugument error

Runtime Error

InvalidPipeArgument: '13:17' for pipe 'DatePipe'

Upvotes: 2

Views: 14243

Answers (2)

Sampath
Sampath

Reputation: 65988

Here you need to use momentjs.

1st Step:

Reference:

npm install moment

ts

import * as moment from 'moment';

2nd Step:

Reference:

moment({ // Options here }).format('hh:mmA')

Upvotes: 1

Mohib Wasay
Mohib Wasay

Reputation: 319

From the documentation

https://angular.io/api/common/DatePipe

expression is a date object or a number (milliseconds since UTC epoch) or an ISO string (https://www.w3.org/TR/NOTE-datetime).

Angular Default DatePipe only has support for Date Objects or ISOStrings(). If you want to convert either into format '01:47PM', the format would be 'hh:mmA'

Upvotes: 2

Related Questions