locked
CRM 2011 : UI Datetime problem RRS feed

  • Question

  • Hi,

    I am using current date time to be set on CRM field in my plug-in as follows.

                    Entity contact = new Entity("contact");
    
                    contact.Attributes.Add("contactid", contactid);
    
                    contact.Attributes.Add("new_lastcontactedon", DateTime.Now);
    
                    service.Update(contact);

    Plug-in is registered in my on-line CRM instance. so when plug-in hits the DateTime.Now it stores the GMT 0.00 Time in database. But when I see that time in UI then it should show the time based on my configured Time zone. But still I am seeing a GMT 0.00 time on my UI.

    So for the record which is processed on GMT 3/27/2013 8:14 AM ( GMT 0.00 ), it should show me 3/27/2013 1:44 PM ( GMT +5.30) on UI.

    Any Ideas?


    Thanks and regards,
    Hiren Solanki.
    Please vote or mark as a answer if it helped you.

    Wednesday, March 27, 2013 8:44 AM

Answers

  • Hi Hiren,

    Yes - When use use the UtcNow, the TimeZone is set to UTC (+00:00) and stored in the database - when it is read in by the UI, the time is then converted to the local time zone from the user settings.

    hth


    Scott Durow
    Read my blog: www.develop1.net/public     Follow Me on Twitter
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    • Marked as answer by Solanki Hiren Wednesday, March 27, 2013 11:02 AM
    Wednesday, March 27, 2013 10:25 AM
    Answerer

All replies

  • Are you trying to set a UTC date time in your plugin or a local time?

    Try using:

    contact["new_lastcontactedon"] = DateTime.UtcNow;

    You might find blog post I did on a related subject useful: http://www.develop1.net/public/post/Dynamics-CRM-DateTimes-the-last-word.aspx

    hth


    Scott Durow
    Read my blog: www.develop1.net/public     Follow Me on Twitter
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Wednesday, March 27, 2013 9:34 AM
    Answerer
  • The server stores all dates in UTC time, and when you use DateTime.Now it is getting the local time from the server. When you update a record through a plugin it is also setting the UTC time, so when a user reads the value on a form, it thinks it is UTC and converts it to local time which means it appears incorrect.

    To fix this you can simply set the date in UTC time using DateTime.UtcNow.

    http://www.magnetismsolutions.com/blog/paulnieuwelaar/2012/03/13/Set_Date_Time_Field_to_Now_in_Plugin_Dynamics_CRM_2011.aspx

    Hope that helps

    Paul


    If my response helped you find your answer please show your thanks by taking the time to "Mark As Answer" and "Vote As Helpful".

    Twitter LinkedIn Facebook Blog Magnetism

    Wednesday, March 27, 2013 9:37 AM
  • Thanks Scott,

    That worked. I think that when we do "DateTime.UtcNow", will it send my current CRM specified time back to CRM  and CRM will store the date by converting to UTC ( +0.00) and when I see on my CRM form I see back the hours adjusted to my time zone specific ?


    Thanks and regards,
    Hiren Solanki.
    Please vote or mark as a answer if it helped you.

    Wednesday, March 27, 2013 10:18 AM
  • Hi Hiren,

    Yes - When use use the UtcNow, the TimeZone is set to UTC (+00:00) and stored in the database - when it is read in by the UI, the time is then converted to the local time zone from the user settings.

    hth


    Scott Durow
    Read my blog: www.develop1.net/public     Follow Me on Twitter
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    • Marked as answer by Solanki Hiren Wednesday, March 27, 2013 11:02 AM
    Wednesday, March 27, 2013 10:25 AM
    Answerer