gpt4 book ai didi

c# - 当 ASP.Net 接收到带有 DateTime 属性的发布模型时,什么决定了 DateTime.Kind?

转载 作者:行者123 更新时间:2023-12-04 10:08:12 25 4
gpt4 key购买 nike

我们有许多带有 Typescript 模型的 Vue 页面,它们提交到相同的 Controller 操作。 Controller 采用单个复杂参数,并且该类具有 DateTime 属性。

Vue 页面已经随着时间的推移进行了修改,现在看起来彼此完全不同。但是,在所有情况下,DateTime 属性都取自 inputDateOfBirth 字段并与 Moment 进行映射。代码看起来有点像这样:

var dob = Helpers.ToMoment(this.inputDateOfBirth);

if (dob.isValid()) {
this.dateOfBirth = dob.format("YYYY-MM-DD HH:mm:ss");
}

两个 dateOfBirthinputDateOfBirth被列为类型 string在 Typescript 模型中。

在大多数情况下,最终由 Controller 操作接收的复杂参数的 DateTime 属性为 DateTime.Kind。的 Utc .然而,其中一个有 DateTime.KindUnspecified .这会导致进一步的问题。

是什么决定了由 ASP.NET MVC 中的 C# Controller 操作解释的数据的 DateTime.Kind 值,我可以做些什么来确保它以 Utc 的形式一致地接收?

最佳答案

日期格式可能会导致这种差异。
DateTime 的默认模型绑定(bind)器需要 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 .

关于c# - 当 ASP.Net 接收到带有 DateTime 属性的发布模型时,什么决定了 DateTime.Kind?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61462849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com