day incorrect in angular material datepicker

后端 未结 15 1876
挽巷
挽巷 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:23

    If the hour of the date is equal to 0, it means we are in the same timezone and you don't need to do something.

    If the hour of the date is greater than 12, it means the date is the day of yesterday and you need to add a day.

    If the hour of the date is lower than 12, it means the date is the day tomorrow and you need to correct it.

    Below, a small function that may helps you as it helps me

    let date = new Date(dateReceived);
    //We use dayCorrector to remove the timezone. We want brut date without any timezone
    let dayCorrector = (date.getHours()>12) ? (1) : (date.getHours()<=12) ? (-1) : (0);
    date.setDate(date.getDate()+dayCorrector);
    let dateFinal = (String(date.getFullYear()) +'-'+ String(date.getMonth()+1) +'-'+ String(date.getDate())+' 00:00:00Z');
    

提交回复
热议问题