javascript - HTML 和 Web API 之间的不同输入数据

标签 javascript angularjs html asp.net-web-api

我正在使用 Angular 和 WebAPI 开发网络应用程序。我有一个奇怪的问题,Angular Controller 和 WebAPI Controller 之间的日期不同。在我的 html 页面上,我有一个日期类型的输入:

 <input id="signupDateofBirth" ng-model="dateofBirth" placeholder="Date of birthday" type="date" max="1998-01-01" min="1900-12-31" class="form-control">

假设日期是 1990 年 12 月 31 日。我正在我的 Angular Controller 中分配它,并且调试日期是正确的:1990年12月31日:

DateOfBirth: $scope.dateofBirth

DateOfBirth: Mon Dec 31 1990 00:00:00 GMT+01

enter image description here

当我在 WebAPI Controller 中获取对象时出现问题。该日期是 Angular Controller 日期的前一天。 1990 年 12 月 30 日,如下图所示: enter image description here

有什么想法吗?也许我需要格式化它?

最佳答案

这是由于时区而出现的。

在您的 Controller 中,日期格式正确,符合您的本地时区 (GMT+1)。

当您的 API 收到它时,它不知道如何处理它:您必须指定 JSON 中返回的日期采用本地时区。

尝试在 APIstartup.cs 中指定您的本地时区,例如使用 newtonsoft JSON :

json.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local;

编辑

您应该始终将 ISO 格式的日期传递给您的 API,反之亦然。请参阅:https://wikipedia.org/wiki/ISO_8601

关于javascript - HTML 和 Web API 之间的不同输入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41246659/

相关文章:

java - 在不同的网页中包含动态创建的 GWT 小部件

javascript - 从 Meteor 中的父访问子模板助手/变量

javascript - 将 angular-ui-select 与 Angular 种子项目结合使用

javascript - 在AngularJS/Electron应用程序中设置$ location

html - 如何将 fontawesome 图标置于位置 :absolute div? 的中心

javascript - 在javascript中将数组分成3组

javascript - TypeError : Object(. ..) 不是一个函数 - 我迷路了

angularjs - 我想在每行之前添加行号,我正在使用 angularJs json 过滤器

javascript - 如何更改 Android WebView 中的 iframe 大小?

html - 使 simple_form 文本字段透明