Quantcast
Channel: Microsoft Dynamics CRM
Viewing all articles
Browse latest Browse all 123975

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

$
0
0

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 = newDateTime();

           //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);

        /// <summary>

        /// Retrieves the current users timezone code

        /// </summary>

        /// <param name="service"> IOrganizationService </param>

        /// <returns></returns>

        privateint? RetrieveCurrentUsersSettings(IOrganizationService service)

        {

            var currentUserSettings = service.RetrieveMultiple(

                newQueryExpression("usersettings")

                {

                  ColumnSet = newColumnSet("timezonecode"),

                    Criteria = newFilterExpression

                    {

                 Conditions =

                        {

                            newConditionExpression("systemuserid", ConditionOperator.EqualUserId)

                        }

                    }

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

            //return time zone code

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

        }

        /// <summary>

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

        /// </summary>

        /// <param name="utcTime">UTC Date time which needs to convert to Local DateTime </param>

        /// <;param name="timeZoneCode">TimeZoneCode </param>

        /// <param name="service">OrganizationService service</param>

        /// <returns></returns>

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

        {

            if (!timeZoneCode.HasValue)

                returnDateTime.Now;

            var request = newLocalTimeFromUtcTimeRequest

            {

                TimeZoneCode = timeZoneCode.Value,

                UtcTime = utcTime.ToUniversalTime()

            };

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

            return response.LocalTime;

        }

        /// <summary>

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

        /// </summary>

        /// <param name="localTime">;Local Date time which needs to convert to UTC</param>

        /// <param name="timeZoneCode">TimeZoneCode </param>

        /// <param name="service">IOrganizationService service</param>

        /// <returns> </returns>

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

        {

            if (!timeZoneCode.HasValue)

                returnDateTime.Now;

            var request = newUtcTimeFromLocalTimeRequest

            {

                TimeZoneCode = timeZoneCode.Value,

                LocalTime = localTime

            };

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

            return response.UtcTime;

        }

     

-------------------------------------------------

Posted by: Inogic

For more information/discussions (documents, sample code snippets, detailed work flow or diagrams)

Please be free to visit the following links or email us:

Web: http://www.inogic.com

Blog: http://inogic.blogspot.com

Email: news@inogic.com

-------------------------------------------------- 


Viewing all articles
Browse latest Browse all 123975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>