0

I'm attempting to display a saved date on a form using an approach that is much like two-way binding for Angular using [ngModel], but not [(ngModel)] utilizing a solution I found on a similar stack overflow question/answer.

However, when I go to save, the date that saves and displays is off by one. For example, if I save using html date input format 07/04/2021 (July 4, 2021) it ends up saving on the form on submit as: 07/03/2021 (the classic off by one error). Below is my code that I took from a previous stack overflow answer. I'm using mongoDB with a typical USER object with a dateOfBirth (Date type) attribute for the date.

<input
   class="form-control"
   id="dateOfBirth"
   type="date"
   name="dateOfBirth"
   placeholder="date"
   [ngModel]="user.dateOfBirth | date: 'yyyy-MM-dd'"
   (ngModelChange)="user.dateOfBirth = $event"
/>

The first approach I tried: Angular 2: How to use JavaScript Date Object with NgModel two way binding

2
  • Could this be a timezone problem? Try adding timezone information to your DataPipe. Commented Oct 15, 2021 at 12:14
  • I want to display the date in MM/DD/YYYY format on the form field. Won't adding timezone information change the way it's displayed? Plus, am I using the DatePipe only for the input and not the output? Commented Oct 15, 2021 at 13:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.