问题描述
我正在使用react-datepicker
选择日期。输出是本地时区的日期。但是,当我将此数据发送到服务器时,当axios在数据对象上调用JSON.stringify()
时,日期将被转换为UTC
,我将获得+-几个小时的时间,具体取决于请求的完成位置来自。
我一直试图解析日期并从中获取UTC日期,但是没有转换时区(我也需要小时数保持不变)。
到目前为止,我发现的唯一方法是将日期转换为toLocaleString
,然后将其输入moment-timezone
。这是唯一的方法吗?遇到所有这些麻烦似乎很奇怪。
console.log(dateToFormat)
let options = { year: 'numeric',month: 'numeric',day: 'numeric',hour: 'numeric',minute: 'numeric' };
const localeDate = dateToFormat.toLocaleString('ro-RO',options);
console.log(localeDate)
const momentDate = moment.tz(localeDate,"DD.MM.YYYY,HH:mm","UTC");
console.log(
momentDate
.format('YYYY-MM-DD HH:mm:ss'),)
console.log(
moment(dateToFormat)
.format('YYYY-MM-DD HH:mm:ss'),)
这是输出:
Fri Aug 14 2020 10:30:00 GMT+0300 (Eastern European Summer Time)
14.08.2020,10:30 - toLocaleString
2020-08-14 10:30:00 - toLocaleString date used to initialize moment and formatted to the date that I need
2020-08-14 07:30:00 - simple moment initialization which has the converted timezone which I don't want because it gives me -3hours. I've also tried '.utc' and '.parse',but without any luck
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)