omkar patade omkar patade - 2 months ago 29
C# Question

How can I prevent a DateTime from getting serialized in XML with culture information?

I am trying to retrieve my data from CRM. But while doing that, it looks like it is applying culture to datetime field.

I have saved the datetime in CRM as - 01/08/2017 12:00 AM.

But when I retrieve this data with the help from FetchXML, its giving me 31/07/2017 06:30 PM

I have set culture in my global.asax file as -

protected void Application_BeginRequest()
CultureInfo info = new CultureInfo(System.Threading.Thread.CurrentThread.CurrentCulture.ToString());
info.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
System.Threading.Thread.CurrentThread.CurrentCulture = info;

and web.config file -

<globalization culture="en-GB" />

Now when i retrieve the data from CRM -

foreach (var item in result2.Entities)
model.SightTestDate = Convert.ToDateTime(item["oph_claimreceiveddate"]);

its giving me this date as - 31/07/2017 06:30 PM

What have i missed here?

Answer Source

Common date fields in records that are queried using the IOrganizationService interface are returned as UTC DateTime values.

You need to convert these values to the proper time zone.