当我们使用 react-datepicker 时,时间在变化

time is changing while we use react-datepicker

i have selected 12:00 Pm in datepicker and i also console log it .... it shows Thu Jul 09 2020 12:00:00 GMT+0530 (India Standard Time) which is right time. but when i click on save then it sends wrong time (2020-07-09T06:30:00.454Z). It sends 5 and half hour less to the time which user selected. It should send (2020-07-09T012:00:00.454Z)----Expected Behaviour i dont know what is the issue and how to fixx this my code

 <DatePicker
        selected={new Date(value)}
        onChange={(e) => setFieldValue(field.name, e)}
        className={classes.datePicker}
        todayButton="Today"
        {...rest}
      />

我需要传递用户选择的完全相同的时间。 我认为这个问题是由于时区或 utc 等引起的。所以请帮忙

这是 javascript 的常见问题。我通常会编写一个“修复时区偏移”函数,并在将代码发送到后端之前调用此函数:

export const fixTimezoneOffset = (date: Date) : string => {
  if(!date) return "";
  return new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toJSON();
}

你这样称呼它:fixTimezoneOffset(my_date)

免责声明:fixTimezoneOffset returns 一个字符串,它将自动计算到后端的日期时间对象中。只调用 fixTimezoneOffset 一次而不是多次!!