简体   繁体   English

C#插件中的日期在Dynamics CRM中相差一天

[英]Dates in C# plugin differ by one day in Dynamics CRM

I've written a C# plugin that reads data from Dynamics CRM but I am having problems with dates. 我已经编写了一个C#插件,该插件可以从Dynamics CRM读取数据,但是日期有问题。 Very often the date coming in from CRM will differ by one day when I process it in the plugin. 在插件中处理CRM的日期通常会相差一天。 Eg 1/7/2017 will become 1/8/2017. 例如1/7/2017将成为1/8/2017。 It has to have something to do with time zones I would think, although the CRM is in the same time zone I am working in. Is there any way to take a date from CRM and use that exact date as read, not adjust it according to time zones or whatnot? 我认为,它与时区有关,尽管CRM与我正在使用的时区相同。是否有任何方法可以从CRM中获取日期并使用所读取的确切日期,而不是根据其进行调整到时区还是什么? I tried monkeying around with UTC date formats but something's not working. 我尝试使用UTC日期格式四处走动,但某些方法无效。

You should read this article . 您应该阅读这篇文章 Highlighting the most important parts: 突出显示最重要的部分:

  • CRM DateTime always comes with time component. CRM DateTime始终带有时间组件。 There is a setting in creating a DateTime field that defines date only, but all that does is default the time to midnight local time 创建DateTime字段时有一个设置,该字段仅定义日期,但是所有设置默认为当地时间午夜12点

  • DateTime is saved in database as UTC time DateTime作为UTC时间保存在数据库中

  • DateTime in CRM UI is always shown based on user's local time zone. CRM UI中的DateTime始终基于用户的本地时区显示。 This is true even if UI is only showing the date component. 即使UI仅显示日期组件也是如此。 This leads to following effect: If user in CST enters contact's birthdate as 2/2/1980, this is saved as 2/2/1980 05:00 in DB If user in PST views the contact record, he/she will see the birthdate as 2/1/1980 (because local time for the user will be 2/1/1980 22:00) 这导致以下效果:如果CST中的用户输入联系人的生日为2/2/1980,则在DB中将其另存为2/2/1980 05:00如果PST中的用户查看联系人记录,则他/她将看到生日。设置为2/1/1980(因为用户的本地时间将为2/1/1980 22:00)

  • DateTime retrieved through CRM Web Services is always UTC time 通过CRM Web服务检索的DateTime始终是UTC时间

  • DateTime set through CRM Web Services is user's local time zone by default 默认情况下,通过CRM Web Services设置的DateTime是用户的本地时区

  • Note that extra care must be taken to understand whether the call is done with the calling user or a service user 注意,必须格外小心,以了解呼叫是由主叫用户还是服务用户完成的

  • DateTime set through CRM Web Services can be defined to be UTC instead 通过CRM Web服务设置的DateTime可以定义为UTC

  • DateTime queried directly from SQL table or base view returns UTC 直接从SQL表或基本视图中查询的DateTime返回UTC

  • DateTime queried from filtered view returns users local time 从过滤视图中查询的DateTime返回用户本地时间

So bascally when you retrieve the date you are getting it in UTC, that's the reason of difference. 因此,当您在UTC中获取要获取的日期时,毫无价值,这就是差异的原因。 To compare dates you should have them both in UTC or simply call ToLocalTime() to convert UTC to your local time. 要比较日期,您应该将它们都设置为UTC或直接调用ToLocalTime()将UTC转换为本地时间。

That is exactly a timezone issue. 那正是时区问题。 Latest CRM version provides TimeZone Independent option for datetime fields to provide a workaround for this kind of issue (you can change the field options in customizations; keep in mind that's an option that cannot be reverted once set). 最新的CRM版本为日期时间字段提供了TimeZone Independent选项,以提供针对此类问题的解决方法(您可以更改自定义项中的字段选项;请记住,该选项一旦设置就无法还原)。

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

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