selected Time from Material TimePicker = '02:18 AM'
.html
<input matInput type="time" [(ngModel)]="inputTime" (ngModelChange)="changeTime()" />
.ts
inputTime: string;
changeTime() {
const hours = moment(this.inputTime, 'HH').hours();
const minutes = moment(this.inputTime, 'HH:mm').minutes();
const meridiem = hours > 12 ? 'PM' : 'AM' ;
this.inputTime = `${hours}:${minutes}` //Output : 2:18
// to get output with leading zero's:
this.inputTime = `${this. setLeadingZero(hours)}:${this. setLeadingZero(minutes)}` //Output : 02:18
Output:
console.log(hours); => //Output : 2
console.log(minutes); => //Output : 18
console.log(meridiem); => //Output : AM
// function to get leading zero's:
setLeadingZero(hour: number) {
return ('0' + hour).slice(-2)
}