UTC time and Local time in CRM 2011

How to convert UTC time into local time in CRM 2011

Sometimes, we need to Convert UTC time into local time for various reasons. If you are in that condition, here is the some lines of code for you to do it.

private static string RetrieveLocalTimeFromUTCTime(DateTime utcTime, string crmUri)
{
ClientCredentials credentials = new ClientCredentials();
credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
Uri organizationUri = new Uri(crmUri);

using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(organizationUri, null, credentials, null))
{
IOrganizationService _serviceProxy = (IOrganizationService)serviceProxy;

var _timeZoneCode = _serviceProxy.RetrieveMultiple(
new QueryExpression(“usersettings”)
{
ColumnSet = new ColumnSet(“timezonecode”),
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression(“systemuserid”, ConditionOperator.EqualUserId)
}
}
}).Entities[0].Attributes[“timezonecode”];

var request = new LocalTimeFromUtcTimeRequest();
request.TimeZoneCode = (int)_timeZoneCode;
request.UtcTime = utcTime.ToUniversalTime();

var response = (LocalTimeFromUtcTimeResponse)_serviceProxy.Execute(request);

return response.LocalTime.ToString();
}
}

Advertisements

Leave a comment

Filed under Convert UTC time into Local time, MS Dynamic CRM 2011

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s