简体   繁体   English

处理javascript和WebApi 2之间的Datetime数据类型

[英]Handling Datetime datatype between javascript and WebApi 2

I would like to know whether the following is the right method to handle datetime data type in WebApi 2, Javascript and database. 我想知道以下是否是在WebApi 2,Javascript和数据库中处理日期时间数据类型的正确方法。

DateTime from Javascript to WebApi: 从Javascript到WebApi的DateTime:

var date = new Date();
var datestring = date.toISOString();
//Send datestring to WebApi

DateTime from WebApi to Javascript: 从WebApi到Javascript的DateTime:

//on getting datetime value from `http.get` call 
var dateFromServer = new Date(dateFromServer); 

WebApi: 的WebAPI:

Incoming date 进入日期

  • do nothing simply store the datestring returned in database column with datatype datetime 什么都不做只是存储数据类型为datetime数据库列中返回的datetime datestring

Getting date from database and Returning date to client: 从数据库获取日期并将日期返回给客户:

  • no datetime manipulation (simply return as per WebApi Json serializer ex: 2015-10-23T18:30:00). 没有日期时间操作(根据WebApi Json序列化程序返回:2015-10-23T18:30:00)。 Client would automatically convert the UTC datetime to local datetime 客户端会自动将UTC日期时间转换为本地日期时间

Yes if you don't want to handle any information about user Timezone etc... this is an acceptable way. 是的,如果您不想处理有关用户Timezone等的任何信息......这是一种可接受的方式。 Just make sure that any time you want a date produced from the server for a comparison or something else to use the c# DateTime.UtcNow method. 只要确保您希望从服务器生成日期以进行比较或其他任何时候使用c# DateTime.UtcNow方法。 I think Having a "Global UTC Convention" its a quite safe and good solution but it has some limits. 我认为拥有“全球UTC公约”是一个非常安全和良好的解决方案,但它有一些限制。

For example if you want to Alert all of your users located in different timezones at 09:00 am (on each user's country) then its impossible to know when its "09:00" for each one. 例如,如果您想在上午09:00(在每个用户的国家/地区)提醒位于不同时区的所有用户,则无法知道每个用户的“09:00”。

One way to solve this(and it's the one i prefer), is to store manually each user's timezone info separately on the database, and every time you want to make a comparison simply convert the time. 解决这个问题的一种方法(也是我更喜欢的方法)是将每个用户的时区信息分别存储在数据库中,每次想要进行比较时只需转换时间。

TimeZoneInfo.ConvertTimeFromUtc(time, this.userTimezone);

Alternatively if you want to store all timezone information on the server you can : 或者,如果要在服务器上存储所有时区信息,您可以:

Send your date from javascript to the server using the following format : "2014-02-01T09:28:56.321-10:00" ISO 8601 also supports time zones by replacing the Z with + or – value for the timezone offset. 使用以下格式将您的日期从javascript发送到服务器: “2014-02-01T09:28:56.321-10:00” ISO 8601还通过将Z替换为时区偏移的+或 - 值来支持时区。

Declare your WEB API 2 Date types with the "DateTimeOffset" type. 使用“DateTimeOffset”类型声明您的WEB API 2日期类型。

Finally store your dates within the database using the "datetimeoffset" type. 最后使用“datetimeoffset”类型将日期存储在数据库中。

This way any time on the server or the database you have all the information about the user's time and timezone. 这样,无论何时在服务器或数据库上,您都可以获得有关用户时间和时区的所有信息。

You will find this article useful 你会发现这篇文章很有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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