Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Answered

Posting datetimes changing their value when received in x++ code -> how to revert back

(0) ShareShare
ReportReport
Posted on by 160
I have the following issue:
 
EXAMPLE:
 
I create an absence request for for example 05-01-2023 from 8:00 AM (FromDateTime) to 11:00 AM (ToDateTime)
I extended the insert method of the absence request table in order to perform logic here.
There I can see that the posted datetimes I receive are:
 
FromDateTime: 05-01-2022 07:00 AM
ToDateTime: 05-01-2023 10:00 AM
 
In other words, D365 corrected the posted dates to reflect my timezone. How can I reverse this and receive the actually posted datetimes?
  • Superbunny Profile Picture
    Superbunny 160 on at
    Posting datetimes changing their value when received in x++ code -> how to revert back
     
    Yes, I checked this information now:
     
    -I click 12:00 AM
    -I receive 11:00 PM in code and the database indeed also shows the time as 11:00
     
    So indeed, the database shows UTC, while I also receive UTC in code.
     
    I managed to get it to work now by using apply instead of remove for the datetimeoffset:
     
           
            utcDatetime dateTimeFromCorrected;
            utcDatetime dateTimeToCorrected;
    
            dateTimeFromCorrected = DateTimeUtil::applyTimeZoneOffset ( this.FromDateTime, DateTimeUtil::getUserPreferredTimeZone());
            dateTimeToCorrected = DateTimeUtil::applyTimeZoneOffset ( this.ToDateTime, DateTimeUtil::getUserPreferredTimeZone());       
            nrOfDays = DatetimeUtil::Date(dateTimeToCorrected) - DatetimeUtil::Date(dateTimeFromCorrected) +1;
     
  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,468 Most Valuable Professional on at
    Posting datetimes changing their value when received in x++ code -> how to revert back
    You didn't get my point. That the type is called utcDateTime says nothing about then value you have there.
     
    For example, I can put 17/11/2023 10:00 to a field, which is my current local time. But it's not UTC value, because my local time isn't in UTC! To get the correct value, I need to subtract one hour and save 9:00.
    In database, I'll see 9:00 (UTC), while in GUI, I'll see 10:00 (the same value converted from UTC to CET).

    Timezones are handled automatically by forms, but it's your responsibility if you write data in code.
     
    Hopefully this gives you some idea about how timezones works. With this knowledge, can you answer my question again, please?
  • Superbunny Profile Picture
    Superbunny 160 on at
    Posting datetimes changing their value when received in x++ code -> how to revert back
    Well it is like I said, I extract the posted dates from the values which are chosen.
    So if I choose datepicker 03 january 12:00 AM until 03 january 11:59 PM and D365 removes an hour, now suddenly my code will assume it is from 02 january 11 PM until 03 january 11:59 PM.
    Now as you can see, this means that the code would count the numerOfDays variables as two, since my timespan now crosses two days.
    That is not correct, as the user (me, since Im checking in a breakpoint) selecting the hours of ONE day and not two days.
     
     
    Yes they are utc datetimes, that is why I need to use DatetimeUtil::Date(this.FromDateTime), as to subtract, I need Dates and not datetimeutc.
     
     
    As for the timezones, I am indeed trying to more or less ignore them. I simply click a datepicker and when I then receive those dates in my code I want them to be the same as the posted date. So I want to reverse this kind of hidden datetime adjustment done by the application. The main reason is, like I said above, I want to extract the chosen daterange (number of days) from the datepicker.
  • Suggested answer
    Waed Ayyad Profile Picture
    Waed Ayyad 6,585 Super User 2024 Season 2 on at
    Posting datetimes changing their value when received in x++ code -> how to revert back
     
    Why don't you want the values to be on your time zone?  
     
     
     
    Regards,
    Waed
  • Martin Dráb Profile Picture
    Martin Dráb 230,468 Most Valuable Professional on at
    Posting datetimes changing their value when received in x++ code -> how to revert back
    You still keep ignoring the topic of timezones.
     
    To simplify things, let's talk for only about what you have in database, which should be in UTC.
     
    So... your code uses values of FormDateTime and ToDateTime fields. Are the value really in UTC? If the input values are wrong, it's expected that the output will be wrong too.
  • Superbunny Profile Picture
    Superbunny 160 on at
    Posting datetimes changing their value when received in x++ code -> how to revert back
    @Waed: I tried adding the following:

    dateTimeFromDateLocal = DateTimeUtil::removeTimeZoneOffset ( this.FromDateTime, DateTimeUtil::getUserPreferredTimeZone());
     
    However, this results in the code subtracting another hour from my time, resulting in a 2 hour difference with the originally posted time instead of the 1 hour difference.
     
  • Superbunny Profile Picture
    Superbunny 160 on at
    Posting datetimes changing their value when received in x++ code -> how to revert back
    Thank you for your replies so far and apologies for not specifying the problem clearly.
     
    What I am doing is creating in absenceRegistration and extending my insert:
     
     public void insert()
     {
            next insert();
            this.insertRelatedTimecardTrans();        
     }
     
     
    Then in my method I wish to count the number of days of the absence registration in order to create a timeCard record for each day:
     
     private void insertRelatedTimecardTrans()
     {
            Counter nrOfDays;
            nrOfDays = DatetimeUtil::Date(this.ToDateTime) - DatetimeUtil::Date(this.FromDateTime) +1;
            for(Counter i = 0; i < nrOfDays; i++)
            {
                //do logic
            }
    }
     
     
    Now when x++ is changing my posted datetime to an hour in the past, this obviously will lead to wrong inserts in my method.
     
    EXAMPLE:
    I select an absence registration with startDateTime 03-01-2023 12:00 AM and endTime 03-01-2023 11:59 PM, save it
    and afterwards I enter my breakpoint in my function in visual studio.
    Here the dates are changed to 02-01-2023 11:00 PM and 03-01-2023 10:59.
     
     
     
  • Suggested answer
    Waed Ayyad Profile Picture
    Waed Ayyad 6,585 Super User 2024 Season 2 on at
    Posting datetimes changing their value when received in x++ code -> how to revert back
    Hi,
     
    Do you want to remove time zone offset? Take a look on this link:
     
    Regards,
    Waed Ayyad
     
     
  • Martin Dráb Profile Picture
    Martin Dráb 230,468 Most Valuable Professional on at
    Posting datetimes changing their value when received in x++ code -> how to revert back
    What are you looking at? Values in database or values displayed in an F&O form?
     
    Database stores datetime values in UTC, but F&O applies user's timezone. This is needed to show the correct time to users in different timezones.
     
    By the way, I moved your question from Dynamics 365 general forum, because it's clearly about a particular product and not about D365 in general. And because you mentioned X++, it seems to be F&O.

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,759 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,468 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans