How to convert the DateTime in Local and UTC Date Time format

By | June 28, 2013

While working with the plug-ins or with Custom Workflows Assemblies, we get the Datetime from CRM which doesn’t match with user’s Local DateTime format.

As we get the date from the CRM in UTC Format so we can convert it to User’s Local DateTime using LocalTimeFromUtcTimeRequest Request.
To get the Current logged in user’s Local time You need to first retrieve the Time Zone code of that user using RetrieveCurrentUsersSettings request and then you need to convert the UTC date into user’s Local DateTime.
We can also convert the Date from Local Datetime format  to the UTC Datetime Format.
Below examples illustrates how to write LocalTimeFromUtcTimeRequest  and UtcTimeFromLocalTimeRequest request to convert the Date.
Example :
First retrieve the time zone code from UserSettings entity and then excecute request LocalTimeFromUtcTimeRequest to convert the DateTime from UTC Format to Local DateTime format and UtcTimeFromLocalTimeRequest to convert the DateTime from Local to UTC DateTime format as given below.
DateTime convertDate = new DateTime();

 

 

           //get the Time Zone Code of user

 

            int? getTimeZoneCode = RetrieveCurrentUsersSettings(service);

 

 

//Convert the UTC Date time into Users Local DateTime Format using request LocalTimeFromUtcTimeRequest

 

            DateTime localDateTime = RetrieveLocalTimeFromUTCTime(convertDate, getTimeZoneCode, service);

 

            //Convert the Local Date time into UTC DateTime Format using request UtcTimeFromLocalTimeRequest

 

            DateTime utcDateTime = RetrieveUTCTimeFromLocalTime(convertDate, getTimeZoneCode, service);

 

 

        ///

 

        /// Retrieves the current users timezone code

 

        /// 

 

        /// IOrganizationService

 

        ///

 

        private int? RetrieveCurrentUsersSettings(IOrganizationService service)

 

        {

 

            var currentUserSettings = service.RetrieveMultiple(

 

                new QueryExpression(“usersettings”)

 

                {

 

                    ColumnSet = new ColumnSet(“timezonecode”),

 

                    Criteria = new FilterExpression

 

                    {

 

                        Conditions =

 

                        {

 

                            new ConditionExpression(“systemuserid”, ConditionOperator.EqualUserId)

 

                        }

 

                    }

 

                }).Entities[0].ToEntity<Entity>();

 

            //return time zone code

 

            return (int?)currentUserSettings.Attributes[“timezonecode”];

 

        }

 

 

        ///

 

        ///  Retrive the local time from the UTC time.

 

        /// 

 

        /// UTC Date time which needs to convert to Local DateTime

 

        /// TimeZoneCode

 

        /// IOrganizationService service

 

        ///

 

        private DateTime RetrieveLocalTimeFromUTCTime(DateTime utcTime, int? timeZoneCode, IOrganizationService service)

 

        {

 

            if (!timeZoneCode.HasValue)

 

                return DateTime.Now;

 

            var request = new LocalTimeFromUtcTimeRequest

 

            {

 

                TimeZoneCode = timeZoneCode.Value,

 

                UtcTime = utcTime.ToUniversalTime()

 

            };

 

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

 

            return response.LocalTime;

 

        }

 

 

        ///

 

        ///  Retrive the UTC DateTime from Local Date time format.

 

        /// 

 

        /// Local Date time which needs to convert to UTC

 

        /// TimeZoneCode

 

        /// IOrganizationService service

 

        ///

 

        private DateTime RetrieveUTCTimeFromLocalTime(DateTime localTime, int? timeZoneCode, IOrganizationService service)

 

        {

 

            if (!timeZoneCode.HasValue)

 

                return DateTime.Now;

 

 

            var request = new UtcTimeFromLocalTimeRequest

 

            {

 

                TimeZoneCode = timeZoneCode.Value,

 

                LocalTime = localTime

 

            };

 

            var response = (UtcTimeFromLocalTimeResponse)service.Execute(request);

 

            return response.UtcTime;

 

        }

Free 70% of storage space in CRM with Attachment Management Apps!

Attach2Dynamics – Store and manage documents/attachments in cloud storage of your choice – SharePoint, Dropbox or Azure Blob Storage from within Dynamics 365 CRM.
SharePoint Security Sync – Robust and secure solution to integrate Dynamics 365 CRM and SharePoint Security Sync thereby ensuring secure access to confidential documents stored in SharePoint.