I'm using datetime picker in primeng to show only the time picker
Here's the code that I have written :
<p-calendar [(ngModel)]="classTimingsArray[i].start_time" timeOnly="true"></p-calendar>
As I have given timeOnly = true, I'm only getting timepicker which is what I want
But the value inside the ngModel is complete date along with the time (Wed Jan 17 2018 02:00:00 GMT+0530 (India Standard Time))
I only want the time to be retrieved
How should I do that?
You can handle the onSelect event in your template:
<p-calendar (onSelect)="onSelect($event)"
[(ngModel)]="classTimingsArray[i].start_time" timeOnly="true">
</p-calendar>
From your component:
timeValue: string;
onSelect($event) {
let hour = new Date($event).getHours();
let min = new Date($event).getMinutes();
if(min < 10) {
this.timeValue= `${hour}:0${min}`;
} else {
this.timeValue= `${hour}:${min}`;
}
}
Demo: https://stackblitz.com/edit/prime-ng-calendar-e8lrz1?file=app%2Fcalendar%2Fcalendar.component.ts
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With