Javascript 如何在react FlatPicker中自动设置当前时间?

Javascript 如何在react FlatPicker中自动设置当前时间?,javascript,flatpickr,Javascript,Flatpickr,我正在使用react flatpicker库作为项目的日期选择器。使用库的问题是,如果我选择任何日期,它总是将时间视为00:00:00。由于这种行为,当来自不同时区的用户保存日期时会产生问题 当用户选择日期时,是否有办法传递用户的本地时间? 我还研究了enableTime:true,它也允许选择时间。此选项也无法设置当前时间 这是我的密码 const options = { enableTime: false, dateFormat: 'd-m-Y' }; <Fl

我正在使用
react flatpicker
库作为项目的日期选择器。使用库的问题是,如果我选择任何日期,它总是将时间视为
00:00:00
。由于这种行为,当来自不同时区的用户保存日期时会产生问题

当用户选择日期时,是否有办法传递用户的本地时间?

我还研究了
enableTime:true
,它也允许选择时间。此选项也无法设置当前时间

这是我的密码

const options = {
  enableTime: false,
  dateFormat: 'd-m-Y'
};

        <Flatpickr
          placeholder="Select date"
          ref={datepickerRef}
          options={options}
          value={props.field.value ? props.field.value : ''}
          onChange={(date) => {
            let selectedDate = new Date(date[0]);
            props.form.setFieldTouched(props.field.name);
            props.form.setFieldValue(props.field.name, selectedDate);
          }}

        />
const选项={
启用时间:false,
日期格式:“d-m-Y”
};
{
设selectedDate=新日期(日期[0]);
props.form.setFieldTouched(props.field.name);
props.form.setFieldValue(props.field.name,selectedDate);
}}
/>
下午6时41分

输出:
2019年10月11日星期五00:00:00 GMT+0530(印度标准时间)


预期输出:
2019年10月11日星期五18:41:00 GMT+0530(印度标准时间)

由于库不支持该格式,我们可以使用
setHours(小时、分钟、秒)
本机方法将自定义时间设置为所选日期

获取当前时间并将这些值设置为所选日期

<Flatpickr
          placeholder="Select date"
          ref={datepickerRef}
          options={options}
          value={props.field.value ? props.field.value : ''}
          onChange={(date) => {
            let selectedDate = date[0];
            let currentDate = new Date();
            let selectedDateWithCurrentTime = new Date(
              selectedDate.setHours(currentDate.getHours(), currentDate.getMinutes(), currentDate.getSeconds())
            );
            console.log('selectedDateWithCurrentTime',selectedDateWithCurrentTime); // Fri Oct 11 2019 19:47:53 GMT+0530 (India Standard Time)
            props.form.setFieldTouched(props.field.name);
            props.form.setFieldValue(props.field.name, selectedDateWithCurrentTime.toISOString());
          }}
        />
{
让selectedDate=日期[0];
让currentDate=新日期();
让selectedDateWithCurrentTime=新日期(
已选择日期.setHours(currentDate.getHours(),currentDate.getMinutes(),currentDate.getSeconds())
);
console.log('selectedDateWithCurrentTime',selectedDateWithCurrentTime);//2019年10月11日星期五19:47:53 GMT+0530(印度标准时间)
props.form.setFieldTouched(props.field.name);
props.form.setFieldValue(props.field.name,selectedDateWithCurrentTime.toISOString());
}}
/>

检查此处的示例

由于库不支持该格式,我们可以使用
setHours(hours,min,sec)
native方法将自定义时间设置为所选日期

获取当前时间并将这些值设置为所选日期

<Flatpickr
          placeholder="Select date"
          ref={datepickerRef}
          options={options}
          value={props.field.value ? props.field.value : ''}
          onChange={(date) => {
            let selectedDate = date[0];
            let currentDate = new Date();
            let selectedDateWithCurrentTime = new Date(
              selectedDate.setHours(currentDate.getHours(), currentDate.getMinutes(), currentDate.getSeconds())
            );
            console.log('selectedDateWithCurrentTime',selectedDateWithCurrentTime); // Fri Oct 11 2019 19:47:53 GMT+0530 (India Standard Time)
            props.form.setFieldTouched(props.field.name);
            props.form.setFieldValue(props.field.name, selectedDateWithCurrentTime.toISOString());
          }}
        />
{
让selectedDate=日期[0];
让currentDate=新日期();
让selectedDateWithCurrentTime=新日期(
已选择日期.setHours(currentDate.getHours(),currentDate.getMinutes(),currentDate.getSeconds())
);
console.log('selectedDateWithCurrentTime',selectedDateWithCurrentTime);//2019年10月11日星期五19:47:53 GMT+0530(印度标准时间)
props.form.setFieldTouched(props.field.name);
props.form.setFieldValue(props.field.name,selectedDateWithCurrentTime.toISOString());
}}
/>
在这里检查示例