locked
CRM 2011 plug-in reading Datefields in utc causing issues RRS feed

  • Question

  • Hello, I have an issue here with plugin reading date-fields. When the plugin reads the crmdatefield, it reads as the utc time (i believe that's how the Datatime is stored by the CRM). 

    Say, I have the CRM date field as 2/25/2012 :00:00 (User is in GMT Lisbon Time ie the local time is GMT+1).

    DateTime getdatefromCRM = entity.GetAttribute<DateTime>("new_crmdatefield");

    Console.writeline("crm field date value" + getdatefromCRM.ToString());

    The getdatefromCRM is displayed as 2/24/2012 : 11PM 

    Though, I am interested in only the date component, however, the time factor causes the error - 1day early.  (though the different between local time and GMT is 1 hour).

    In between the local time from  00:00 through 23:59, whenever the plug-in reads the CRM date field, the date component should say 2/25/2012 and not the 2/24/2012. 

    Appreciate if anyone could share their thoughts on this. Thanks

    Thursday, April 5, 2012 4:59 PM

Answers

  • Hi, what you need to do is to convert the new_crmdatefieldfield to the local time using as below

    You can get timezone code from the usersettings. 

    var request = new LocalTimeFromUtcTimeRequest
                                {
                                    TimeZoneCode = timeZoneCode,
                                    UtcTime = new_crmdatefield.ToUniversalTime()
                                };
    
                                var response = (LocalTimeFromUtcTimeResponse)service.Execute(request);

    This will ensure that the plug-in reads the datefield correctly. In your example, it will read as 2/25/2012 12AM not as 2/24/2012 11:PM. 

    Hope this helps. 

    • Proposed as answer by Seepath Thursday, April 5, 2012 5:37 PM
    • Marked as answer by CRM elite Thursday, April 5, 2012 6:11 PM
    Thursday, April 5, 2012 5:37 PM

All replies

  • Hi, what you need to do is to convert the new_crmdatefieldfield to the local time using as below

    You can get timezone code from the usersettings. 

    var request = new LocalTimeFromUtcTimeRequest
                                {
                                    TimeZoneCode = timeZoneCode,
                                    UtcTime = new_crmdatefield.ToUniversalTime()
                                };
    
                                var response = (LocalTimeFromUtcTimeResponse)service.Execute(request);

    This will ensure that the plug-in reads the datefield correctly. In your example, it will read as 2/25/2012 12AM not as 2/24/2012 11:PM. 

    Hope this helps. 

    • Proposed as answer by Seepath Thursday, April 5, 2012 5:37 PM
    • Marked as answer by CRM elite Thursday, April 5, 2012 6:11 PM
    Thursday, April 5, 2012 5:37 PM
  • That worked. Thanks a lot Seepath. I appreciate it. 
    Thursday, April 5, 2012 6:11 PM