After integrating the angular bootstrap datepicker
component into my application, I encountered an issue with converting the selected date object {year: 1993, month: 8, day: 9}
into a Date
object. The conversion is successful, but the resulting date is shifted: 1993-09-08T22:00:00.000Z
. Any suggestions on how to maintain the original date during conversion?
Upon investigation, I discovered that this discrepancy is due to the difference in the starting date numbers between angular bootstrap (starting from 1) and the Date object (starting from 0).
For the HTML setup:
<div class="col-md-12 mb-3 form-group">
<label for="dateOfBirth">Data urodzenia</label>
<input class="form-control"
placeholder="yyyy-mm-dd" id="dateOfBirth"
formControlName="dateOfBirth"
name="dp"
ngbDatepicker
#d="ngbDatepicker"
(click)="d.toggle()"
[minDate]="fromDate"
[maxDate]="toDate">
<span class="input-group-addon">
<i class="glyphicon glyphicon-calendar"></i>
</span>
<ngb-alert [type]="'danger'"
[dismissible]="false"
class="form-alert"
*ngIf="envelopeCreationFrom.controls.dateOfBirth.touched &&
envelopeCreationFrom.controls.dateOfBirth.errors?.required">
Error
</ngb-alert>
</div>
And for the Typescript code:
console.log(dateOfBirth);
console.log(new Date(dateOfBirth['year'], dateOfBirth['month'], dateOfBirth['day']));
console.log(new Date(dateOfBirth['year'], dateOfBirth['month'], dateOfBirth['day']).toISOString());
console.log(new Date(dateOfBirth['year'], dateOfBirth['month'], dateOfBirth['day']).toDateString());