简体   繁体   中英

What determines the DateTime.Kind when ASP.Net receives a posted model with a DateTime property?

We have a number of Vue pages, with Typescript models, that submit to the same controller action. The controller takes a single complex parameter, and the class features a DateTime property.

The Vue pages have been modified over time and now look quite unlike each other. However, in all cases the DateTime property is taken from an inputDateOfBirth field and mapped with Moment. The code tends to look a bit like this:

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

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

Both dateOfBirth and inputDateOfBirth are listed as type string in the Typescript model.

In most cases, the complex parameter that ends up being received by the controller action has this DateTime property as DateTime.Kind of Utc . One of them, however, has a DateTime.Kind of Unspecified . This causes issues further down the line.

What determined the DateTime.Kind value of data interpreted by a C# controller action in ASP.NET MVC, and what can I do to ensure it's received consistently as Utc?

Format of the date can causes this difference.
Default model binder for DateTime expects ISO 8601 Time zone designators , which means that if your date string looks like this 2009-06-15T13:45:30.0000000Z , then DateTime.Kind will be UTC as “Z” presented.
Or if like this 2009-06-15T13:45:30.0000000-07:00 , where timezone offset “-07:00” is presented, DateTime.Kind will be Local .
If none of this attributes is presented, DateTime.Kind will be Unspecified .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM