web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Datetime issue

(0) ShareShare
ReportReport
Posted on by

Hi,

Can anyone help me regarding my problem in DateTime?

Here's my scenario:

I have 3 record, a Level 1, Level 2 and Level 3.

When I update the Level 3, the Est. Start Date and Est End Date of Level 1 and 2 must capture the field data in Level 3. But when the plugin runs, the date before the selected date in Level 3 reflects in Level 1 and 2. see image below

x.png

So if I select 12/28 in Level 3, 12/27 will reflect in Level 1 and 2.

Here's my code for Start Date

if (c.Attributes.Contains("new_eststartdate"))
                            {
                                
                                if (dateEstStart == new DateTime(1753, 01, 01, 00, 00, 00))                               
                                    dateEstStart = ((DateTime)c.Attributes["new_eststartdate"]).ToLocalTime();                                                                                                        

                                currEstStart = ((DateTime)c.Attributes["new_eststartdate"]).ToLocalTime();                                    

                                dtStartResult = DateTime.Compare(currEstStart, dateEstStart);
                                if (dtStartResult < 0)
                                    dateEstStart = currEstStart;
                            }
DateTime newStartDate = new DateTime(1753, 01, 01, 00, 00, 00); newStartDate = dateEstStart; if (dateEstStart != new DateTime(1753, 01, 01, 00, 00, 00)) ParentEntity.Attributes["new_eststartdate"] = newStartDate;


Can anyone help me about this?

Thank you very much!

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at
    RE: Datetime issue

    Hi Guido,

    Thank you for your quick response.

    Here's one example,

    I created a Level 3 record and entered 12/28/2015 in it's Est. Start Date field. When I save Level 3, the parent Level 2 and Level 1 must copy the Est. Start Date of Level 3. But what happen in my current case is that 12/27/2015 (the date before 12/28/2015) reflects in Level 2 and Level 1.

  • Verified answer
    Royal King Profile Picture
    27,686 on at
    RE: Datetime issue

    Are you running this custom code in plugin or workflow?

    We run into same kind of issue with real time workflow and we resolved it by just considering the date and specifying datetimekind

    var result = new DateTime();

        result = start.Date; // removes time portion 

    result = DateTime.SpecifyKind(result, DateTimeKind.Local).ToUniversalTime();  //specifying the datetime of kind local

  • Verified answer
    Community Member Profile Picture
    on at
    RE: Datetime issue

    In a plugin, when you read a date field using sdk, the datetime field will be retrieved as datetimekind.utc. but the field passed in the pluginexecutioncontext due to the user action like selecting a date in the form will be in datetimekind.local. So you just need to make sure that you pass the date as a datetimekind.local instead of utc. I guess this will solve your issue.

    By the way, I think you can remove few lines of code like below:

    dateEstStart = ((DateTime)c.Attributes["new_eststartdate"]).ToLocalTime();                                                                                                        

                                   currEstStart = ((DateTime)c.Attributes["new_eststartdate"]).ToLocalTime();                                    

                                   dtStartResult = DateTime.Compare(currEstStart, dateEstStart);

    dateEstStart and currEstStart are both assigned with the same values. So I don't see a point in comparing each of them.

    Also instead of initializing the date like below:

    DateTime newStartDate = new DateTime(1753, 01, 01, 00, 00, 00); You can use

    DateTime newStartDate = DateTime.MinValue.

  • Community Member Profile Picture
    on at
    RE: Datetime issue

    Hi Chitra,

    Thank you very much for this. This solved my problem.

  • Community Member Profile Picture
    on at
    RE: Datetime issue

    Hi Ratheesh,

    Thanks for your reply.

    Didn't use Datetime.MinValue coz CRM don't allow it. I used the DateTimeKind.Local to fix my issue. Thank you very much

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Community Member Profile Picture

Community Member 2

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans