Проблема Crm DateTime в плагине

У меня вопрос, связанный с проблемой CRM DateTime. В форме возможности есть настраиваемое поле DateTime (Дата подачи тендера), которое отображается в форме «Только дата». И другое строковое поле (Дата тендера), которое изменяет дату при изменении даты подачи тендера. Допустим... Дата подачи тендера 29.06.2011 12:00:00 Дата тендера должна быть 29.06.2012

Я создаю плагин для Create Post-operation и Update Pre-operation. Я извлекаю TenderSubDate.Day, Month и Year.
Часовой пояс Crm: (GMT+08:00) Куала-Лумпур, Сингапур, затем хочу изменить (GMT-06:00) центральное время (США и Канада).

Проблема в том, что когда я обновляю дату тендера на основе даты подачи тендера, программа возвращает на один день меньше или больше, чем дополнительная дата тендера. Скажем..

Первый секнарио

Дата подачи тендерных предложений 29.06.2012 12:00:00.

Программа возвращает 28/06/2012(ошибочно, должно быть 29/06/2012)

Второй секнарио

Дата подачи тендерных предложений 08.01.2012 12:00:00.

Программа возвращает 32/07/2012 (ошибочно, должно быть 08/01/2012)

что мне делать в моей программе. пожалуйста, дайте мне некоторое представление. Вот мой код плагина

public class TenderSubDateChange : IPlugin
{
    #region Class Level Variables
    //IServiceProvider _serviceProvider;
    //IOrganizationServiceFactory _serviceFactory = null;
    //IOrganizationService _service = null;
    //IPluginExecutionContext _context = null;

    Entity _target = null;
    Entity _preImage = null;
    Entity _postImage = null;
    Guid _currentUser;
    #endregion

    #region  IPlugin Members
    public void Execute(IServiceProvider serviceProvider)
    {
        try
        {
            string message = null;
            IPluginExecutionContext _context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            #region Organization Services
            // Obtain the organization service reference.
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(_context.UserId);
            #endregion

            var ServiceContext = new OrganizationServiceContext(service);

            _currentUser = _context.UserId;
            message = _context.MessageName.ToLower();

            if (message == "create")//message == "create" || 
            {
                if (_context.InputParameters.Contains("Target") && _context.InputParameters["Target"] != null)
                    _target = (Entity)_context.InputParameters["Target"];

                if (_context.PreEntityImages.Contains("PreImage") && _context.PreEntityImages["PreImage"] != null)
                    _preImage = (Entity)_context.PreEntityImages["PreImage"];

                if (_context.PostEntityImages.Contains("PostImage") && _context.PostEntityImages["PostImage"] != null)
                    _postImage = (Entity)_context.PostEntityImages["PostImage"];

                DateTime hm_tenderdate;
                if (_target.Attributes.Contains("hm_tendersubmissiondate"))
                {
                    hm_tenderdate = (DateTime)_target.Attributes["hm_tendersubmissiondate"];
                    _target.Attributes["hm_tendersubdate"] = (hm_tenderdate.Day) + "/" + hm_tenderdate.Month + "/" + hm_tenderdate.Year;
                    service.Update(_target);
                }
            }
            if (message == "update")//message == "create" || 
            {
                if (_context.InputParameters.Contains("Target") && _context.InputParameters["Target"] != null)
                    _target = (Entity)_context.InputParameters["Target"];

                if (_context.PreEntityImages.Contains("PreImage") && _context.PreEntityImages["PreImage"] != null)
                    _preImage = (Entity)_context.PreEntityImages["PreImage"];

                if (_context.PostEntityImages.Contains("PostImage") && _context.PostEntityImages["PostImage"] != null)
                    _postImage = (Entity)_context.PostEntityImages["PostImage"];

                DateTime hm_tenderdate;
                if (_target.Attributes.Contains("hm_tendersubmissiondate"))
                {
                    hm_tenderdate = (DateTime)_target.Attributes["hm_tendersubmissiondate"];
                    _target.Attributes["hm_tendersubdate"] = (hm_tenderdate.Day) + "/" + hm_tenderdate.Month + "/" + hm_tenderdate.Year;
                }
            }
        }
        catch (Exception ex)
        {
            throw new InvalidPluginExecutionException(ex.Message, ex);
        }
    }
    #endregion
}

введите здесь описание изображения


person Nightmare    schedule 27.06.2012    source источник
comment
Также обратите внимание, что вы можете использовать hm_tenderdate.Date для получения компонента даты DateTime (в полночь) вместо того, чтобы собирать его самостоятельно.   -  person John M. Wright    schedule 02.07.2012


Ответы (1)


Я сталкивался с этой проблемой раньше и сводил меня с ума, и решение состоит в том, чтобы преобразовать время UTC в местное время.

DateTime tenderDate = ((DateTime)target["new_tender"]).ToLocal();
person Anwar    schedule 27.06.2012