convert string to Time format in typescript

10,481

Solution 1

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'

Solution 2

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')
Share:
10,481
ionMobDev
Author by

ionMobDev

Interested to keep tracking on new challenges and supporting to resolve the critical bugs until resolving the issues.

Updated on July 16, 2022

Comments

  • ionMobDev
    ionMobDev almost 2 years

    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'

    • Niladri
      Niladri over 6 years
      can you post your code ?
    • Niladri
      Niladri over 6 years
      the input of the date pipe should be an expression which is a date object or a number (milliseconds since UTC epoch) or an ISO string (https://www.w3.org/TR/NOTE-datetime).