Sunday, 5 April 2015

GetLocalTime in mscrm

 public static DateTime GetLocalTime(ITracingService tracing, IOrganizationService service, IPluginExecutionContext context, DateTime UTCTime)
      {
          tracing.Trace("In GetLocalTime method");
          LocalTimeFromUtcTimeResponse timeResp = new LocalTimeFromUtcTimeResponse();
          try
          {
              // If UTCTime is null the return UTCTime
              if (UTCTime.Equals(DateTime.MinValue))
                  return UTCTime;

              // Create LocalTimeFromUtcTimeRequest instance and set the UtcTime and TimeZoneCode
              LocalTimeFromUtcTimeRequest timeReq = new LocalTimeFromUtcTimeRequest();
              timeReq.UtcTime = UTCTime.ToUniversalTime();
              timeReq.TimeZoneCode = GetUserTimeZoneCode(tracing, service, context);

              // Execute the LocalTimeFromUtcTimeRequest
              timeResp = (LocalTimeFromUtcTimeResponse)service.Execute(timeReq);

          }
          catch (Exception ex)
          {
              tracing.Trace("Error in GetLocalTime method: " + ex.Message);
          }

          return timeResp.LocalTime;
      }

No comments:

Post a Comment