Reputation: 41
I've written a C# plugin that reads data from Dynamics CRM but I am having problems with dates. Very often the date coming in from CRM will differ by one day when I process it in the plugin. E.g. 1/7/2017 will become 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? I tried monkeying around with UTC date formats but something's not working.
Upvotes: 0
Views: 4445
Reputation: 3586
You should read this article. Highlighting the most important parts:
CRM DateTime always comes with time component. 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 is saved in database as UTC time
DateTime in CRM UI is always shown based on user’s local time zone. This is true even if UI is only showing the date component. 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)
DateTime retrieved through CRM Web Services is always UTC time
DateTime set through CRM Web Services is user’s local time zone by default
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
DateTime queried directly from SQL table or base view returns UTC
DateTime queried from filtered view returns users local time
So bascally when you retrieve the date you are getting it in UTC, that's the reason of difference. To compare dates you should have them both in UTC or simply call ToLocalTime()
to convert UTC to your local time.
Upvotes: 2
Reputation: 23300
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).
Upvotes: 1