Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从前端发布到web api问题的日期_Javascript_Asp.net Web Api - Fatal编程技术网

Javascript 从前端发布到web api问题的日期

Javascript 从前端发布到web api问题的日期,javascript,asp.net-web-api,Javascript,Asp.net Web Api,我有一些带有小时和分钟文本框字段的日历,用于在我的angular应用程序中捕获日期时间 将其转换为完整日期,如下所示: accidentVM.AccidentNotificationDateFull = new Date(accidentVM.AccidentNotificationDate.getFullYear(), accidentVM.AccidentNotificationDate.getMonth(), accidentVM.AccidentNotificationDate.getD

我有一些带有小时和分钟文本框字段的日历,用于在我的angular应用程序中捕获日期时间

将其转换为完整日期,如下所示:

accidentVM.AccidentNotificationDateFull = new Date(accidentVM.AccidentNotificationDate.getFullYear(), accidentVM.AccidentNotificationDate.getMonth(), accidentVM.AccidentNotificationDate.getDate(),
                                                parseInt(accidentVM.AccidentNotificationHour), parseInt(accidentVM.AccidentNotificationMinute), 0);
var notificationDate = accidentVM.AccidentNotificationDateFull;
现在,如果我将此notificationDate作为web api中的输入发布,当它到达web api时,它会变得不同,因为在我的例子中,javascript附加了我的本地时区(即+5:30)

为了避免这种情况

我从客户端对GMT进行了调整。这基本上是记下GMT偏移量,然后从输入中减去或加上它,这样当它到达服务器时,它会再次变得正确

函数看起来像

this.convertDate = function (dateVM) {               
                var dateObj = dateVM;
                if (new Date().getTimezoneOffset() < 0) {
                    dateVM = new Date(dateObj.setTime(dateObj.getTime() - new Date().getTimezoneOffset() * 60 * 1000));
                }
                else {
                    dateVM = new Date(dateObj.setTime(dateObj.getTime() + new Date().getTimezoneOffset() * 60 * 1000));
                }                
};
this.convertDate=函数(dateVM){
var dateObj=dateVM;
如果(新日期().getTimezoneOffset()<0){
dateVM=新日期(dateObj.setTime(dateObj.getTime()-new Date().getTimezoneOffset()*60*1000));
}
否则{
dateVM=新日期(dateObj.setTime(dateObj.getTime()+新日期().getTimezoneOffset()*60*1000));
}                
};
这解决了问题,我现在看到了正确的日期和时间被发布,但是它仍然在服务器中到达时在末尾附加了一个T

请参见此处前端的输入

当它到达web api时,它就变成了

和相应的小提琴手请求

如果您注意到它正在变成UTC或在末尾附加Z


我的问题是,如果有任何方法可以摆脱Z或更具体地说,DateTime种类应该是unspecified/local?

我在WebApiConfig类中添加了以下本地日期格式设置。这将告诉web api json格式化程序转换为本地时区

var jsonFormatter = config.Formatters.JsonFormatter;
jsonFormatter.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local;

因此,我不需要GMT的显式客户端操作。

ISO 8601是以字符串形式传输日期的最佳格式。您的后端只需读取字符串并将其解析为瞬间。然后,您可以应用您想要的任何时区