0
votes

Recently, I tried to get datetime value from custom field in Invoice entity throuhg plugin and found that it returns a day before the date displayed in CRM form. For incstance, I entered "7/1/2013" and in my C# the following code returns "6/30/2013":

EntityReference eRef = new EntityReference(localContext.PluginExecutionContext.PrimaryEntityName, localContext.PluginExecutionContext.PrimaryEntityId);
var invoice = localContext.OrganizationService.Retrieve(eRef.LogicalName, eRef.Id, new ColumnSet(true));
string start = ((DateTime)invoice["revg_startdate"]).ToString(); //here I get 6/30/2013

Then I checked the actual datetime in relevant MS SQL table, and it keeps exactly a day before, "6/30/2013"

The date, time and timezones are absolutely identical on server, my PC and in CRM.

Why CRM keeps and displays different dates?

1

1 Answers

2
votes

Dynamics CRM stores all dates in the database as UTC time. The user interface the converts this time based on the users local time zone from the usersettings.

The reason for this is that you could have users in different time zones working on the same CRM organization, and CRM have to store the dates in a common format to be able to show the correct date and time to all users .

If you want the local time in a plugin you could use .ToLocalTime()

string start = ((DateTime)invoice["revg_startdate"]).ToLocalTime().ToString();