2

I have datepicker:

    <input
        type="text"
        formControlName="startDate"
        [ngClass]="{ 'has-error': form.get('startDate').invalid }"
        [matDatepicker]="picker"
        class="materialDatePickerInput"
        placeholder="{{ 'select_date' | translate }}"
      />
      <mat-datepicker-toggle
        matSuffix
        [for]="picker"
      ></mat-datepicker-toggle>
      <mat-datepicker #picker></mat-datepicker>

I have selected date: 01.01.2000 from datepicker, but it sends to server like:

1999-12-31T20:00:00.000Z

Why it converts to wrong date?

POV
  • 11,293
  • 34
  • 107
  • 201
  • 1
    see this anwser https://stackoverflow.com/questions/7556591/is-the-javascript-date-object-always-one-day-off – samuel gast Nov 26 '19 at 07:22
  • 1
    All you need to do is set the timezone, like following: https://stackoverflow.com/questions/439630/create-a-date-with-a-set-timezone-without-using-a-string-representation – samuel gast Nov 26 '19 at 07:26
  • https://stackoverflow.com/questions/49699411/angular-material-mat-datepicker-change-event-and-format/49699536#49699536 – Vikas Nov 26 '19 at 10:23

1 Answers1

2

It does not. As mentioned in the comments, it's a timezone issue. Let me explain it.

You are getting the time in the UTC format on your server side which is mostly correct. We tend to do that so that we can store time in a specific format (often in ticks) so that we can do math on it without worrying about conversion.

My guess is that your own timezone is UTC + 2. So what happens is that you choose a date, the date picker selects the day with 00 time in your own timezone and when the time is converted on your backend then 2 hours are missing.

It's not another date or the wrong one, it's just another timezone.

If you need date time then you need to decide what timezone/ locale to choose in your front end and backend.

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61