day incorrect in angular material datepicker

后端 未结 15 1857
挽巷
挽巷 2020-12-08 11:01

When I select a date I see the correct date in the field but, when I save, the datepicker send the day before the date I have selected ( 3 hours offset ) i am using angular

15条回答
  •  眼角桃花
    2020-12-08 11:31

    The solution stackblitz works great, but if you are not storing the timestamp in Database, then you need to convert back the date to UTC format while retrieving it from database before assigning to the datepicker.

    let dbDate = new Date('2018-02-09T00:00:00.000Z');
    
    let updateDate = new Date(
          Date.UTC(dbDate.getFullYear(), dbDate.getMonth(), dbDate.getDate())
        ).toISOString(); // 2018-02-08T00:00:00.000Z
    

    now assign the updatedDate to the datepicker, hope this helps.

提交回复
热议问题