简体   繁体   中英

How to display date-time in HH:MM AM format in Ionic

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

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.

You can do like this:

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

Read more here from ionic docs

如果你想在 ap 标签中“显示”它,那么你可以使用momentJs 库否则,如果你想将它加载到 ionic 的 dateTime 指令中,请遵循 kyle 发布的任何内容

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

如果要显示为“dd/MM/yyyy HH:mm am/pm”格式,只需使用:

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

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM