[英]What determines the DateTime.Kind when ASP.Net receives a posted model with a DateTime property?
我们有许多 Vue 页面,带有 Typescript 模型,它们提交到相同的 controller 操作。 controller 采用单个复杂参数,而 class 具有 DateTime 属性。
Vue 页面已经随着时间的推移进行了修改,现在看起来彼此完全不同。 但是,在所有情况下,DateTime 属性都取自 inputDateOfBirth 字段并与 Moment 进行映射。 代码看起来有点像这样:
var dob = Helpers.ToMoment(this.inputDateOfBirth);
if (dob.isValid()) {
this.dateOfBirth = dob.format("YYYY-MM-DD HH:mm:ss");
}
dateOfBirth
和inputDateOfBirth
在 Typescript model 中都列为类型string
。
在大多数情况下,最终由 controller 操作接收的复杂参数具有此 DateTime 属性作为DateTime.Kind
of Utc
。 然而,其中一个有一个DateTime.Kind
的Unspecified
。 这会导致进一步的问题。
是什么决定了由 C# controller 操作在 ASP.NET MVC 中解释的数据的 DateTime.Kind 值,我可以做些什么来确保它被一致地接收为 Utc?
日期格式可能会导致这种差异。
DateTime 的默认 model 活页夹需要ISO 8601 Time zone designators
,这意味着如果您的日期字符串看起来像这样2009-06-15T13:45:30.0000000Z
,那么DateTime.Kind
将是UTC
,如“Z”所示。
或者,如果像这样2009-06-15T13:45:30.0000000-07:00
出现时区偏移“-07:00”,则DateTime.Kind
将是Local
。
如果没有提供这些属性,则DateTime.Kind
将是Unspecified
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.