I have a string in the form of time that I get from an object, which I format with datePipe to display it as follows:
But when I click some of the hours which is when I'm calling the click event, this gives me the following error:
And it is that the user is shown 12:00 PM but behind it you have to convert it to the dateTime type "2017-12-14T16:34:10" since that is how it should be sent to the server.
This is the HTML where I loop through the object for the time and format it:
<swiper class="TimeContainer" [config]='swiperConfig' (swiper)="onSwiper($event)" (slideChange)="onSlideChange()">
<ng-template swiperSlide class="SwiperItem text-center" *ngFor="let slot of slots; let i = index">
<button *ngIf="selectedTime == slot.start" mat-stroked-button>{{slot.start | date: 'shortTime'}}
</button>
<button *ngIf="selectedTime != slot.start" mat-flat-button color='primary' class="SelectedTime" (click)="setTime(slot.start)" [disabled]="!slot.available || getHoursPased(slot.start)">{{slot.start | date: 'shortTime'}}
</button>
</ng-template>
</swiper>
//Evento click que se llama cuando se selecciona una hora
setTime(start: any) {
this.selectedTime = start;
console.log('select time', start);
this.convertToDatetime();
}
convertToDatetime() {
var formattedDate = this.selectedDate?.toLocaleDateString("en-US");
var dt: Date = new Date(formattedDate + " " + this.selectedTime);
this.bookingService.bookingModel.appointmentDate = dt;
//let myDate = new Date();
}
What is the correct way to convert "7:00 AM" to 2021-12-27T07:00:00?
please if someone can help me
Valid date assignments are as follows:
To achieve what you want you must do it on your own by building the hour. This is one option:
A solution can be to use the moment library, it gives a better possibility of handling dates and allows the conversions that you request without error with the template pipe
HTML