**
UPDATED ANSWER
**
Here's a StackBlitz that solves your issues now completely. https://stackblitz.com/edit/pm-am-time-conversion
Below I have pasted the whole code but you can find my comments. In summary I implemented the reactive form so I can make things workable. For the editable case, you obviously would be getting data from some server but in order to reproduce it I have just done this to mock:
I have taken dates as a class property and In ngOnInit method let { startDate, endDate } = this.dates; and then setting the Form Controls in order to populate the dropdowns.
In the TypeScript file you will see that I am using two generic methods for time conversions:
parseFromAmPmToTwentyFour(): For AM/PM time to 24 Hours time and parseFromTwentyFourToAmPm(): For 24 Hours time to AM/PM time
A generic method for handling both start date and end date dropdown
onSelect(): For setting startDate and endDate in the dropdown and tracking class properties
HTML
<h2>{{ title }}</h2>
<form [formGroup]="dateTrackForm">
Start at:
<select
formControlName="startDate"
(change)="onSelect($event.target.value, 'startDate')"
id="skill">
<option value="0" selected>Select</option>
<option [value]="type" *ngFor="let type of startTimeArray; let i = index">
{{type}}
</option>
</select>
End at:
<select
formControlName="endDate"
(change)="onSelect($event.target.value, 'endDate')">
<option value="0" selected>Select</option>
<option [value]="type" *ngFor="let type of endTimeArray">
{{type}}
</option>
</select>
</form>
TypeScript
export class AppComponent implements OnInit {
public title = "Nested FormArray Example Add Form Fields Dynamically";
public startTimeArray: string[] = [
"7:00 AM", "8:00 AM", "9:00 AM", "10:00 AM", "11:00 AM", "12:00 PM", "1:00 PM", "2:00 PM", "3:00 PM", "4:00 PM", "5:00 PM", "6:00 PM", "7:00 PM", "8:00 PM",
];
public endTimeArray: string[] = [
"7:00 AM", "8:00 AM", "9:00 AM", "10:00 AM", "11:00 AM", "12:00 PM", "1:00 PM", "2:00 PM", "3:00 PM", "4:00 PM", "5:00 PM", "6:00 PM", "7:00 PM", "8:00 PM",
];
public dates = {
startDate: '08:00',
endDate: '13:00',
}
// form name
public dateTrackForm: FormGroup;
// keeping track from startDate and endDate
public startDate: string;
public endDate: string;
constructor(
private fb: FormBuilder
) { }
ngOnInit(): void {
// response from some api, you obviously have to do some work
let { startDate, endDate } = this.dates;
this.dateTrackForm = this.fb.group({
startDate: this.fb.control(''),
endDate: this.fb.control('')
});
if (startDate) {
this.startDate = startDate;
this.dateTrackForm.get('startDate').patchValue(this.parseFromTwentyFourToAmPm(startDate));
}
if (endDate) {
this.endDate = endDate;
this.dateTrackForm.get('endDate').patchValue(this.parseFromTwentyFourToAmPm(endDate));
}
}
// we will using same method for both the case and will identify start or end dates based on the second prameter for which I have defined an enum at the end of the file
public onSelect(time: string, date: Dates): void {
if (date === Dates.START_DATE) {
this.startDate = this.parseFromAmPmToTwentyFour(time);
} else if (date === Dates.END_DATE) {
this.endDate = this.parseFromAmPmToTwentyFour(time);
}
// 24 hours time that you want already assigned to respective field and now you can simple do what you want with it.
}
// This method will parse the time from am/pm time to 24 hours time
public parseFromAmPmToTwentyFour(time: string): string {
let hours = Number(time.match(/^(\d+)/)[1]);
const minutes = Number(time.match(/:(\d+)/)[1]);
const AMPM = time.match(/\s(.*)$/)[1];
if (AMPM == "PM" && hours < 12) hours = hours + 12;
if (AMPM == "AM" && hours == 12) hours = hours - 12;
let sHours = hours.toString();
let sMinutes = minutes.toString();
if (hours < 10) sHours = "0" + sHours;
if (minutes < 10) sMinutes = "0" + sMinutes;
return `${sHours} : ${sMinutes}`;
}
// This method will parse the time from 24 hours time to am/pm time
public parseFromTwentyFourToAmPm(time: string): string {
const H = +time.substr(0, 2);
const h = H % 12 || 12;
const ampm = (H < 12 || H === 24) ? "AM" : "PM";
time = h + time.substr(2, 3) + ' ' + ampm;
return time;
}
}
// enum for detecting start or end date
enum Dates {
START_DATE = "startDate",
END_DATE = "endDate",
}
Here's a StackBlitz that solves your issues now completely.
https://stackblitz.com/edit/pm-am-time-conversion
Source for AM:PM to 24 Hours conversion: convert 12-hour hh:mm AM/PM to 24-hour hh:mm
Source for 24 Hours to AM:PM conversion:
Javascript: convert 24-hour time-of-day string to 12-hour time with AM/PM and no timezone
The existing solutions that I have used are the same and I have done some changes.
One thing I would like to mention is that you have just declared things and there was a lot of missing implemention like Reactive Form was just declared but wasn't used. In order to provide you with the solution I have to add the missing implementations too. Please before asking any such questions do try to add all the stuff you can.
Thanks to @Heretic Monkey for advising to updating the answer.