如何使用时区格式化URL datetime参数 [英] How to format url datetime parameter with timezone

查看:36
本文介绍了如何使用时区格式化URL datetime参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我到处搜索,找不到答案.我想了解格式化日期时间(带有时区)URL参数的方式是怎么回事.

I've searched everywhere and can't find the answer. I would like to understand what's going with the way I format the datetime (with timezone) url parameter.

这里是情况:

  1. 呼叫者程序的DateTime值为UTC时区.
  2. 接收器Json WebAPI(C#)正在具有中央时区的本地PC上运行.
  3. PoCreationDate是DateTime类型(C#).我不编写代码来解析PoCreationDate值.C#自动将其转换为DateTime对象(通过序列化?).

以下是测试案例:

案例1 :这个有效

http://******/api/ItemSource/GetItemSourceOption?OrganizationCode = OKC& PoCreationDate = 2018-10-08T01:02:03.0000000-05:00

http://******/api/ItemSource/GetItemSourceOption?OrganizationCode=OKC&PoCreationDate=2018-10-08T01:02:03.0000000-05:00

案例2 :这一项不起作用,浏览器正在显示

Case 2: This one does not work and the browser is displaying

<Error>
    <Message>The request is invalid.</Message>
</Error>

http://******/api/ItemSource/GetItemSourceOption?OrganizationCode = OKC& PoCreationDate = 2018-10-08T05:00:00.0000000 + 00:00

http://******/api/ItemSource/GetItemSourceOption?OrganizationCode=OKC&PoCreationDate=2018-10-08T05:00:00.0000000+00:00

注意到有什么不同吗?其中一个具有-05:00,另一个具有+00:00.我的时区是中部(现在是-05:00?)

Notice the different? one of them have a -05:00 the other have +00:00. My timezone is Central (which is -05:00 right now?)

案例3 :我目前的解决方法是将其格式化http://******/api/ItemSource/GetItemSourceOption?OrganizationCode = OKC& PoCreationDate = 2018-10-08T05:00:00 Z

Case 3: My current work around is to format it this way http://******/api/ItemSource/GetItemSourceOption?OrganizationCode=OKC&PoCreationDate=2018-10-08T05:00:00Z

====================

=====================

所以我只是想了解这里发生的事情以及这些想法...

So I am just trying to understand what's going on here and these my thoughts...

我相信使用Z格式是最好的解决方案,因为DateTime值(来自源)始终为UTC格式.

I believe that using the Z format is the best solution since the DateTime value (from the source) is always in UTC format.

关于-05:00和+00:00,是否应该按照接收者的时区(目标服务器本地时区)设置这些值?因此,对于这种情况,我的PC是接收器(WebAPI),并且将其设置为中央时区",因此该值必须为-05:00才能代表中央时区"的当前值吗?

About the -05:00 and +00:00, are these supposed to be set per the receiver's timezone (the destination Server local timezone)? So for this case, my PC is the receiver (WebAPI) and it's set to Central Time Zone, therefore this value must be -05:00 to represent the current value for Central Time Zone?

请帮助我理解这一点.谢谢.

Please help me understand this. Thanks.

推荐答案

根据URL编码规则,偏移量中的 + 字符被解释为空格.您将需要对其进行编码,以便将其替换为%2B .

The + character in the offset is being interpreted as a space, as per URL encoding rules. You will need to encode it, such that it is replaced with %2B.

如果要使用C#构建它,请使用

If you are building it in C#, use the System.Net.WebUtility.UrlEncode method.

请注意,这样做还将用%3A 替换:字符,这在querystring参数中是可选的,但仍建议这样做.

Note that doing so will also replace the : characters with %3A, which is optional in a querystring parameter, but still recommended.

通常,需要对查询字符串中传递的参数进行编码,除非您可以保证它们不包含特殊字符.

In general, parameters passed in the querystring need to be encoded, unless you can guarantee that they contain no special characters.

此外,您可能想问自己,该字段是否确实需要完整的日期+时间+偏移量.在许多情况下,人们可能希望像 PoCreationDate 这样的字段成为 just 日期,就像"2018-10-08" 中那样.当然,这取决于您的应用程序逻辑和业务需求.

Also, you may want to ask yourself if this field really needs to be a full date+time+offset. In many cases, one might expect a field like PoCreationDate to be just a date, as in "2018-10-08". Of course, that depends on your application logic and business requirements.

这篇关于如何使用时区格式化URL datetime参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆