Angular Material Datepicker时区忽略吗? [英] Angular Material Datepicker Timezone ignore?
问题描述
我正在尝试从用户那里获取航班的日期,而datepicker始终将日期设置为具有用户时区的datetime对象,导致发送给服务器的日期错误.
尝试使用ng-model-options="{ timezone: 'utc' }"
I'm trying to get a date for flights from the user and the datepicker always sets the date as datetime object with the user's timezone, resulting in a wrong date sent to server.
tried to use ng-model-options="{ timezone: 'utc' }"
但是,然后用户在选择日期后看到错误的日期(日期显示为用户选择的日期的前一天).
but then the user see the wrong date after choosing the date (the date displayed as a day before what the user chose).
我如何告诉datepicker完全忽略时区-如果不可能,如何在FE/BE上将该日期转换为非时区日期?
How can I tell datepicker to completely ignore timezone - if not possible what can I do on FE / BE to convert this date to non timezone date ?
我知道我可以在FE上将其转换为Posix,但是一旦完成,日期选择器就会发出尖叫,提示它需要日期对象.
I know I can convert it to Posix on FE but once I do it the datepicker screams that it needs date object.
预先感谢...
推荐答案
我遇到了完全相同的问题,并且我使用了这段代码结束了我们
I had exactly same issue and I ended us using this code
Date.prototype.toJSON = function () {
var timezoneOffsetInHours = -(this.getTimezoneOffset() / 60); //UTC minus local time
var sign = timezoneOffsetInHours >= 0 ? '+' : '-';
var leadingZero = (timezoneOffsetInHours < 10) ? '0' : '';
//It's a bit unfortunate that we need to construct a new Date instance
//(we don't want _this_ Date instance to be modified)
var correctedDate = new Date(this.getFullYear(), this.getMonth(),
this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds(),
this.getMilliseconds());
correctedDate.setHours(this.getHours() + timezoneOffsetInHours);
var iso = correctedDate.toISOString().replace('Z', '');
return iso + sign + leadingZero + Math.abs(timezoneOffsetInHours).toString() + ':00';
}
来自另一个SO问题如何使用JSON对javascript日期进行字符串化并保留时区
这篇关于Angular Material Datepicker时区忽略吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!