locked
Daylight saving is not being acommodated in the returned DateTime (using CRM Services) RRS feed

  • Question

  • Hi All,

    I am trying to pull appointment data using CRM data services.  I am not able to get the correct timings in Appointment Start and End date time. The offset is 1 hour which is based on current user settings, however "daylight saving" is not being acommodated in the returned DateTime.

    Below is the code that i am using:

    IEnumerable<UserSettings> userSettings = null;
                userSettings = (from userSet in serviceContext.UserSettingsSet
                               where userSet.SystemUserId.Value == UserId
                               select userSet).ToList();
                if (userSettings != null)
                {
                    if (userSettings.Count() > 0)
                    {
                        if (userSettings.First() != null)
                        {
                            int? timeZoneBias = userSettings.First().TimeZoneBias;
                            if (timeZoneBias != null)
                            {
                                dateTime = ((DateTime)dateTime).AddMinutes((double)(timeZoneBias * -1));
                            }
                            if (userSettings.First().TimeZoneDaylightBias != null)
                            {
                                dateTime = ((DateTime)dateTime).AddMinutes((double)(userSettings.First().TimeZoneDaylightBias * -1));
                            }
                        }
                    }
                }
                return dateTime;

    Regards,

    Pranav

    Monday, March 25, 2013 12:52 PM

Answers

  • Hi,
    the datetime values inside CRM 2011 are stored using UTC format. Did you try to use LocalTime?

    Entity appointment;
    // retrieve the appointment
    DateTime scheduledStartUTC = (DateTime)appointment["scheduledstart"];
    DateTime scheduledEndUTC = (DateTime)appointment["scheduledend"];
    // returns the datetime as local time
    DateTime scheduledStart = scheduledStartUTC.ToLocalTime();
    DateTime scheduledEnd = scheduledEndUTC.ToLocalTime();


    My blog: www.crmanswers.net


    Monday, March 25, 2013 1:34 PM

All replies

  • Hi,
    the datetime values inside CRM 2011 are stored using UTC format. Did you try to use LocalTime?

    Entity appointment;
    // retrieve the appointment
    DateTime scheduledStartUTC = (DateTime)appointment["scheduledstart"];
    DateTime scheduledEndUTC = (DateTime)appointment["scheduledend"];
    // returns the datetime as local time
    DateTime scheduledStart = scheduledStartUTC.ToLocalTime();
    DateTime scheduledEnd = scheduledEndUTC.ToLocalTime();


    My blog: www.crmanswers.net


    Monday, March 25, 2013 1:34 PM
  • Where are you getting the dateTime value from ? If it's returned from Crm, then the calling user's timezone settings should already have been applied. I don't see the need for you to apply the timezone settings from UserSettings

    Also, what type of .Net project (e.g. ASP .Net, Silverlight, Console Application) is the code in, and what AD user context does it run under ? It could be that you could use the .Net framework ToLocalTime() method, though it depends on AD user context


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Monday, March 25, 2013 1:37 PM
    Moderator
  • Hi Guido... using ToLocalTime() resolved my problem for now...however it still needs to be tested for different TimeZones.

    thanks :)

    Tuesday, March 26, 2013 6:36 AM