5

I have a date string "2017-01-19 10:34:36", it's coming from API.

I want to display it in HH:MM AM format in Ionic

6 Answers 6

11

To display datetime, you can just do it in angular2 way, using a pipe. Angular2 provides a DatePipe. You can use it like this:

{{info.createdDate | date: "yyyy/MM/dd HH:mm:ss"}}

[update]

If you want to display as format 'HH:MM AM', just use:

{{info.createdDate | date: "shortTime"}}

will be fine.

1
  • 1
    Question is "HH:MM AM" format. This will display date and 24 hour time format.
    – JanP
    Apr 6, 2017 at 10:00
3

You can do like this:

<ion-datetime displayFormat="hh:mm a" [(ngModel)]="myDate"></ion-datetime>

Read more here from ionic docs

1

If you want to "display" it in say a p tag then you can use momentJs library Otherwise, if you want to load it into the dateTime directive of ionic follow whatever kyle has posted

1

A good resource is the angular documentation. It is well written check it out here https://docs.angularjs.org/api/ng/filter/date

1

If you want to display as format 'dd/MM/yyyy HH:mm am/pm', just use:

{{item.createdDate | date: "dd/MM/yyyy HH:mm a"}}
0

Pass the date string to following function, it will return time in HH:MM AM format.

getFormatedTime(dateString){
   var date = new Date(dateString);
   var hours = date.getHours() > 12 ? date.getHours() - 12 : date.getHours();
   var am_pm = date.getHours() >= 12 ? "pm" : "am";
   var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
   let time = hours + ":" + minutes + " " + am_pm;
   return time;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.