DateTime in CRM 2011 RRS feed

  • Question

  • I'm encountering a problem in programming with DateTime in plugin for CRM 2011.

    In CRM 4, UTC value is stored inside the database. When CrmDateTime is returned upon data retrieving, we can use CrmDateTime.UserTime to get the native DateTime based on CRM user setting and CrmDateTime.UniversalTime to get the native DateTime which value is UTC.

    Also on creating or updating the CrmDateTime, we can use CrmTypes.CreateCrmDateTimeFromUniversal() and CrmTypes.CreateCrmDateTimeFromUser().

    Lets say, we have two users; user A has GMT+2 and user B has GMT+3. CRM Server has GMT+2 (same as A).
    Somewhere in plugin, I want to stamp the DateTime as now. I can use

    CrmDateTime stampDate = CrmTypes.CreateCrmDateTimeFromUniversal(DateTime.Now.ToUniversalTime());

    Here, DateTime.Now will return the server time. Since there is two users having different time zones, creating the DateTime from UTC is the safest way.

    For the above case, if the server is 10am, user A would see 10am and for user B it'd be 11am after updating the stamp date. In side the database, the value will be 8am (UTC value).

    In retrieving, we can compare against the current date time in server like the following.

    CrmDateTime stampDate = entity["stamp"] as CrmDateTime;
    if (DateTime.Compare(stampDate.UniversalTime, DateTime.Now.ToUniversalTime()) > 0)
    //do something here

    But in CRM 2011, it goes by native DateTime. When the date time is updated, it always counts as current user time. I coudn't find the way to specify that the value is UTC.

    entity["stamp"] = DateTime.Now;

    It's working fine for user A (who has the same time zone as server) but not working for user B (has +1 more than server).

    For the above case, if the server is 10am, user A should see 10am and for user B it'd be 11am after updating the stamp date. In side the database, the value should be 8am (UTC value).

    But the acutal value insie the database is 9am.

    When I tried

    entity["stamp"] = DateTime.Now.ToUniversalTime();

    it's getting worse. Since I convert it to UTC, CRM still thinks it's as user time and getting more different value.

    Is there any way to achieve like the way I used in CRM 4?

    Tuesday, February 22, 2011 3:33 AM

All replies

  • Hello LilMaster,

    In CRM 4.0, I used to convert DateTime to CrmDateTime and viceversa.

    To convert from DateTime to CrmDateTime, use the following code:

    DateTime dt = DateTime.Now;
    dt = DateTime.Now;
    CrmDateTime dateTime = new CrmDateTime();
    dateTime.Value = dt.ToString("s");

    To convert from CrmDateTime to DateTime, use the following code snippet:


    Also check this article from CRM SDK: http://msdn2.microsoft.com/en-us/library/aa613549.aspx

    Cornel Croitoriu - Senior Software Developer & Entrepreneur

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    CWS SoftwareBiz-Forward.comCroitoriu.NET

    Tuesday, February 22, 2011 2:49 PM
  • I suppose we mis-understand each other.

    Converting CrmDateTime to DateTime is working fine in CRM 4. But in CRM 2011, there is no more use of CrmDateTime.

    In CRM 4, when we assign value to CrmDateTime value, we can differentiate whether it's creating from UTC or current user time. Even users have different time zone, as long as we know UTC time and can specify CrmDateTime to assume as UTC, we won't have any problem.

    In CRM 2011, I cannot find that feature. As a result, when I try to stamp in DateTime attribute using current time and if the current user has different time zone from server, then the result went wrong.

    Wednesday, February 23, 2011 1:28 AM
  • Moving to Development to see if you can gain some additional exposure.

    Regards, Donna

    Monday, February 28, 2011 6:21 PM
  • Did anyone have the solution? Please.
    Monday, March 7, 2011 9:08 AM
  • I am able to reproduce this. This is a serious issue for any customers with users in different locations. Perhaps someone from Microsoft can shed some light on how to address this problem.

    Tuesday, March 8, 2011 1:39 PM
  • If you would like to retrieve data and work in your (windows) client timezone, you can use the "ToLocalTime" Method of Datetime.

    DateTime dtNew = dtFromCrm.ToLocalTime();

    If you would like to use the Timezone of the CRM user, you have to read the UserSettings.


    If you would like to store user specific data (Calendar...) you have to retrieve the user Settings.

    1. Retrieve UserSettings (Contains TimeZoneCode)

    2. Retrieve UTC time with TimeConeCode of user and the UtcTimeFromLocalTimeRequest

    3. Store the date as UTC


    Kind regards,


    Tuesday, March 8, 2011 4:44 PM
  • I might have misunderstood the topic. My issue had to do with Creating and Updating dates using the REST endpoint with javascript. I have recently found the solution to that.

    LilMaster if your code is using C# Markus reply should work.

    If your issue is while using Javascript and the REST Endpoint then this is how we fixed it.

    If your dealing with date only and you use something like this         new Date(2011, 2, 10)

    1. Pull the from the SystemUserSer ".../SystemUserSet(guid'{123123123-213123}')/user_settings
    2. The TimeZoneBias node contains the offset in minutes
    3. Add the offset to the Date.
    Thursday, March 10, 2011 4:08 PM
  • Sokha, I am doing the same as you have mentioned But this does not address the daylight issue. For example, when I retrieve a date attribute in Silverlight, I get it in UTC formated. The formattedValue of the same date attribute returns a string. And Crm users have ability to individually change the format in which they want to see datetime and currency. Hence, cannot use DateTime.Parse on that string value. So I am using the TimeZoneBias node's value to add to the UTC offset. However, it does not work for daylight savings time. It shows one hour less.

    Friday, April 29, 2011 8:48 PM
  • Ahmad Pirani, the difference between summer and winter time is a variable TimeZoneDaylightBias .

    It remains only to determine the date of transition from summer to winter :)

    • Edited by Maksym Tomyn Wednesday, November 23, 2011 9:58 AM
    Wednesday, November 23, 2011 9:57 AM
  • I'm not sure, but I think you can use the following:

    entity["stamp"] = DateTime.UtcNow;

    I haven't tried it, but I think i saw it somewhere...

    Thursday, May 31, 2012 7:19 PM