处理 javascript 和 WebApi 2 之间的日期时间数据类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33340908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 16:22:30  来源:igfitidea点击:

Handling Datetime datatype between javascript and WebApi 2

javascriptc#datetimeasp.net-web-api

提问by Shyamal Parikh

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:

DateTime 从 Javascript 到 WebApi:

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

DateTime from WebApi to Javascript:

从 WebApi 到 Javascript 的日期时间:

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

WebApi:

网络接口:

Incoming date

来料日期

  • do nothing simply store the datestringreturned in database column with datatype datetime
  • 什么都不做简单地将datestring返回的数据存储在数据类型的数据库列中datetime

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). Client would automatically convert the UTC datetime to local datetime
  • 没有日期时间操作(只需根据 WebApi Json 序列化程序返回:2015-10-23T18:30:00)。客户端会自动将 UTC 日期时间转换为本地日期时间

采纳答案by Anestis Kivranoglou

Yes if you don't want to handle any information about user Timezone etc... this is an acceptable way. 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.UtcNowmethod. I think Having a "Global UTC Convention" its a quite safe and good solution but it has some limits.

是的,如果您不想处理有关用户时区等的任何信息……这是一种可以接受的方式。只需确保任何时候您想要从服务器生成日期以进行比较或使用 c# DateTime.UtcNow方法。我认为拥有“全球 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

你会发现这篇文章很有用