Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

DateTime problems in Workflow not converting to local time.

(0) ShareShare
ReportReport
Posted on by 187

I had an issue with a Workflow that was retrieving a DateTime from a plugin and updating the entity. The problem was the DateTime being updated wasn't correct. I've created an example to demonstrate the problem.

I have a test entity with two DateTime fields and a String field. One DateTime field is TimeZone Indenpendt, the other is User Local. I run an on-demand workflow that gets the current DateTime and returns it in DateTime and String format.

public class WorkflowTimingTest : ActivityBase
    {
        [Output("Time")]
        public OutArgument<DateTime> Time { get; set; }
        [Output("Time String")]
        public OutArgument<string> TimeString { get; set; }
        public override void Execute(CodeActivityContext context, LocalContext localContext)
        {
            DateTime dtm = DateTime.Now;
            TimeString.Set(context, dtm.ToString());
            Time.Set(context, dtm));
        }
    }

The Workflow simply updates the Test entity.

Workflow-Timing.PNG


When I run this Workflow and update the record, this is what I see.

Workflow-Timing-2.PNG

As you can see, the plug-in itself didn't acknowledge that I used DateTime.Now (Not UTC Now, which I've tried to use and provides the same outcome). The CRM also didn't convert the time to local, leaving both times as GMT. (We're in EST currently observing DST at -4 GMT).

Can someone explain this? I need to be able to return a specific time from a workflow, this doesn't seem possible without manually subtracting 4 hours (and then changing this to manually subtract 5 hours when DST ends).

*This post is locked for comments

  • Suggested answer
    Pawar Pravin  Profile Picture
    5,231 on at
    RE: DateTime problems in Workflow not converting to local time.

    Hi JFulfordMS,

    Please refer below code to convert date time into local timezone.

    int? getTimeZoneCode = RetrieveCurrentUsersSettings(service); // Pass IOrganizationService service object here

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

    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"];

           }

           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;

           }

  • Suggested answer
    gdas Profile Picture
    50,089 Moderator on at
    RE: DateTime problems in Workflow not converting to local time.

    Hi ,

    Read below  article -

    www.develop1.net/.../Dynamics-CRM-DateTimes-the-last-word

  • Suggested answer
    Charles Abi Khirs Profile Picture
    3,569 on at
    RE: DateTime problems in Workflow not converting to local time.

    Hello,

    As Kokulan suggested, use the method .ToLocalTime() in order to have the right date time in CRM when updating the field.

    It will convert the datetime value to your local timezone and then correctly display it in CRM.

  • Suggested answer
    Kokulan Profile Picture
    18,054 on at
    RE: DateTime problems in Workflow not converting to local time.

    When Plugin or Custom Workflow activity executes, it executes in the user's context. You should be able to use ToLocalTime method.

    Could you try the following and see what results you get?

    DateTime dtm = DateTime.Now.ToLocalTime()

  • JFulfordMS Profile Picture
    187 on at
    RE: DateTime problems in Workflow not converting to local time.

    This link doesn't refer to the issue I'm having.

    A DateTime.Now returned from a plug-in and updated using a Workflow does not conform to 'User Local' timezone formatting rules. The same DateTime is updated into two separate DateTime Fields, one that is 'User Local', the other that is 'Time Zone Independent'. Both of these fields show the same, incorrect DateTime.

    In the plug-in I've used both DateTime.Now and DateTime.UtcNow, both produce the same DateTime and same results. It seems that no matter what I'm passing to the CRM, it is storing it as UTC and refusing to convert to User Local.

    Imagine that you want to schedule an email to go out at a specific DateTime based on an update to an entity. You execute a plug-in that returns the exact DateTime that the email needs to be sent and you set that DateTime as the Timeout condition. Unfortunately, in my current situation, no matter what time my plug-in returns the execution will happen 4 hours AFTER it should have. In order to compensate for this, I'd have to change my plug-in to do DateTime.Now.AddHours(hourDifferenceFromUtc). This seems like a hacky work-around, especially considering we do not always observe DST and sometimes we're 5 hours off from GMT and not 4.

  • Suggested answer
    Prashant_ Profile Picture
    1,040 on at
    RE: DateTime problems in Workflow not converting to local time.

    As per my understanding you need to update DateTime field value using plugin and there is need to update time zone during storage of data .Try conversion rule for Date Time field.In following blog you will get idea about it.

    www.inogic.com/.../updating-date-values-of-existing-records-after-modifying-the-datetime-behaviour-in-dynamics-crm-online-update-1

  • JFulfordMS Profile Picture
    187 on at
    RE: DateTime problems in Workflow not converting to local time.

    I have a DateTime User Local on that entity that isn't displaying correctly. I take a DateTime, commit to the system and it is being shown as UTC in a 'User Local' and 'Time Zone Independent' field.

    I understand that the system runs 'globally' so it should always utilize UTC. However, the fields in CRM specifically designed to provide local time, do not when updated via plug-in or workflow.

  • Suggested answer
    Prashant_ Profile Picture
    1,040 on at
    RE: DateTime problems in Workflow not converting to local time.

    DateTime behaviors is different in CRM because in Dynamics 365 when datetime field value stored as UTC format.When any user try to access it this value convert to users local time zone.

    Now coming to your problem try to  change your DateTime field to User Local.This might help to store value users local time zone however I am not sure about plugins behavior because plugins execute in different server and that might create issue for date time.

    Following blog will help you to understand Date Time format.

    https://community.dynamics.com/crm/b/exploringdynamics365/archive/2017/12/18/dates-in-dynamics-timezone-independent-vs-user-local

    This blog help you to solve issue of date time

    https://www.inogic.com/blog/2015/06/handling-datetime-fields-in-microsoft-dynamics-crm-update-1/

     

    I hope this will help you.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,140 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,895 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans