Friday, April 26, 2013
Convert from one time zone to another time zone - convert Source DateTime to UTC and will then convert UTC timezone to destination timezone code
The function below will first convert Source DateTime to UTC and will then convert UTC timezone to destination timezone code
# region "ConvertFromSourceTimeZoneToDestinationTimeZone"
/// <summary>
/// </summary>
/// <param name="sourceDateTime"></param>
/// <param name="sourceTimeZoneCode"></param>
/// <param name="destinationTimeZoneCode"></param>
/// <param name="organizationServ"></param>
/// <returns></returns>
private DateTime ConvertFromSourceTimeZoneToDestinationTimeZone(DateTime sourceDateTime, int sourceTimeZoneCode, int destinationTimeZoneCode, IOrganizationService organizationServ)
{
DateTime destinationDateTime = DateTime.UtcNow;
try
{
//Convert Source DateTime to UTC
UtcTimeFromLocalTimeRequest utcFromLocalRequest = new UtcTimeFromLocalTimeRequest();
utcFromLocalRequest.LocalTime = sourceDateTime;
utcFromLocalRequest.TimeZoneCode = sourceTimeZoneCode;
UtcTimeFromLocalTimeResponse utcFromLocalResponse = (UtcTimeFromLocalTimeResponse)organizationServ.Execute(utcFromLocalRequest);
//Convert UTC DateTime to Destination TimeZone
LocalTimeFromUtcTimeRequest localFromUtcRequest = new LocalTimeFromUtcTimeRequest();
localFromUtcRequest.UtcTime = utcFromLocalResponse.UtcTime;
localFromUtcRequest.TimeZoneCode = destinationTimeZoneCode;
LocalTimeFromUtcTimeResponse localFromUtcResponse = (LocalTimeFromUtcTimeResponse)organizationServ.Execute(localFromUtcRequest);
destinationDateTime = localFromUtcResponse.LocalTime;
}
catch (FaultException<OrganizationServiceFault>)
{
throw;
}
catch (Exception)
{
}
return destinationDateTime;
}
# endregion
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment