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 :
Finance | Project Operations, Human Resources, ...
Answered

Handle utc dates in x++ d365 finance and operation

(1) ShareShare
ReportReport
Posted on by 177
Hello all,
I've an issue in dates.
I have calculations for payroll between utc dates (employment start / end dates) and i got un expected results,
In employment history form the date is different from the database validFrom date that is stored in employment table.
When investigating i found that the difference is always 2H because my timezone is 2H behind the standard time zone because the datetime is stored in database always in the standard datetime, but that make the employment date in another day if it was 12AM, so that makes wrong calculations for payroll.
So my questions how to handle UTC dates in code if there are calculations based on it.
In code i directly use validFrom date in payroll equations.
Thanks in advance :)
Categories:
I have the same question (0)
  • Verified answer
    Sohaib Cheema Profile Picture
    47,713 User Group Leader on at
    Handle utc dates in x++ d365 finance and operation
    Your utcDateEmployment is in UTC, and you are extracting the date before converting it to your local time. If the UTC time is X, in your local timezone (UTC-2), that UTC Time becomes as  X minus 2 Hours(because you are 2 hours behind UTC). That is why you have the wrong day. Well the result is never wrong mathematically, as it depends upon how you want to view it (your local time or the UTC time)
    If you want days as per your local time-zone, first Convert utcDateEmployment to local time, and then extract the date:
     
     
  • Verified answer
    Martin Dráb Profile Picture
    235,871 Most Valuable Professional on at
    Handle utc dates in x++ d365 finance and operation
    The result is correct. The UTC value is 2-9-2025 22:00:00. Your code extracts the date number, which is two.
     
    Where you're making a mistake is when you're saying that "utcDateEmployment is 3-9-2025 00:00:00", while you're also saying that the actual value in database is 2-9-2025 22:00:00.

    What you actually seem to mean when saying "utcDateEmployment is 3-9-2025 00:00:00" is that you see this value in UI,  i.e. it's the UTC value converted to user's timezone. If you want to get the result in code, you must have this conversion to user's timezone, but your code currently doesn't do it. It works with the UTC value.

    The code you need is something like this:
    utcDateTime employmentInUserTZ = DateTimeUtil::applyTimeZoneOffset(utcDateEmployment, DateTimeUtil::getUserPreferredTimeZone());
    date dateEmployment = DateTimeUtil::date(employmentInUserTZ);
     
  • MS-29011540-0 Profile Picture
    177 on at
    Handle utc dates in x++ d365 finance and operation
    @Martin Dráb


    Thanks Martin for your reply.
    Could you please help me in this scenario
    I have utcDateEmployment;
    And I want to get the day number of it, so i use the following:
    I first convert it to date type, then get the day of that date
    Date DateEmployment = DateTimeUtil::date(utcDateEmployment);
    Int dayNumber =  dayOfMth(DateEmployment );

    I get dayNumber wrong, the code gives me dayNumber 2, and utcDateEmployment is 3-9-2025 00:00:00
    When i check the database i found it 2-9-2025 22:00:00

    How can i handle this case
  • Verified answer
    Martin Dráb Profile Picture
    235,871 Most Valuable Professional on at
    Handle utc dates in x++ d365 finance and operation
    The data type is called UTCDateTime because the values in database should be stored in UTC. What users see in UI is converted to their local timezones (and therefore users in different timezones will see different values in UI for the same value in database). In code, you need to take this into account when you want to show a value to a user (you need to apply user's timezone to the UTC value) or when you want to store a value in a local timezone to database (you must convert it to UTC). Comparing two datetime values in database is a different case - you just compare the UTC values; timezones of users play no role there.
     
    If you have a date stored without a timezone information, you must interpret it as a date in a particular timezone (it's up to you to decide which one). And if you want to compare it with a UTCDateTime value, you need to convert the UTC value to the other timezone.
     
    You'll find necessary methods in DateTimeUtil class, such as applyTimeZoneOffset().
     
    Moved from Integration, Dataverse, and general topics forum to Finance | Project Operations, Human Resources, AX, GP, SL forum.

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…

Mansi Soni – Community Spotlight

We are honored to recognize Mansi Soni as our August 2025 Community…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Sohaib Cheema Profile Picture

Sohaib Cheema 745 User Group Leader

#2
Martin Dráb Profile Picture

Martin Dráb 597 Most Valuable Professional

#3
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 580 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans