Codeblooded Saiyan
Codeblooded Saiyan

Reputation: 1497

convert time to iso format to populate ion-datetime in ionic 2

I have this in my html:

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

I want to populate that with the data from the server. The result from http request is in string format:

10:00PM

I my .ts I have this code to populate:

this.time = data.time;

In which data.time = '10:00PM'

But unfortunately, it give me an Error: invalid ISO Format.

How can I convert 10:00PM to ISO Format in order to populate the ion-datetime with display format of hh:mm a?

Please help me. Thanks :)

Upvotes: 1

Views: 921

Answers (1)

sebaferreras
sebaferreras

Reputation: 44659

If you want to show '10:00PM', you'd need to convert that string to '22:00'. With that format, the ion-datetime component will preselect the '10:00' hour and the 'PM'.

You can easily do that with moment.js, like this:

moment(yourString, 'h:mm a').format('H:mm');

Then, just like you've said, you can adjust the displayFormat attribute to be hh:mm A like this:

<ion-item no-padding color="light">
  <ion-label floating>{{ 'FIELDS.TIME' | translate }}</ion-label>
  <ion-datetime ... displayFormat="hh:mm A"></ion-datetime>
</ion-item>

Upvotes: 1

Related Questions