Datetime to Date. It’s not so easy.
How can you convert datetime variable to date? Some of you can say: “It is easy. Use DT2DATE() function”. But what will be the result? What we will see in Nav user interface and what will be actually stores in the database? What if we are going to call function using web services?
Let’s conduct some experiments.
I work on computer with Russian time setting (UTC+03:00)
I created a table with 3 fields (“ID”, "DateTime Field" and "Date Field") and simple codeunit to fill a record with ID = 0;
It just converts datetime data to date:
OnRun()
InDate := CREATEDATETIME(190617D, 232300T);
Test(InDate);
Test(InDateTime : DateTime)
temp.GET(0);
temp."DateTime Field" := InDateTime;
temp."Date Field" := DT2DATE(InDateTime);
temp.MODIFY;
It turned out that result would depend on:
- How you call test function. From Nav code or using web service.
I used the following SOAP request:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tes="urn:microsoft-dynamics-schemas/codeunit/TestDateTime">
<soapenv:Header/>
<soapenv:Body>
<tes:Test>
<tes:inDateTime>2017-06-19T23:23:00</tes:inDateTime>
</tes:Test>
</soapenv:Body>
</soapenv:Envelope>
- The setting "Services Default Time Zone" on General Tab of our instance
Possible values for this field you can find here
https://msdn.microsoft.com/en-us/library/hh169363(v=nav.90).aspx
To understand how it works on the edges of the day I put it in two tables:
At the end of day:
Input datetime variable = 19.06.2017 23:23
Input Data |
what we see in Nav |
what stores in the database |
|||
Function is called |
Service Default Time Zone Setting |
DateTime Field |
Date Field |
DateTime Field |
Date Field |
from code |
UTC |
19.06.2017 23:23 |
19.06.2017 |
19.06.2017 20:23 |
19.06.2017 |
from web service |
UTC |
20.06.2017 02:23 |
19.06.2017 |
19.06.2017 23:23 |
19.06.2017 |
from code |
Server Time Zone |
19.06.2017 23:23 |
19.06.2017 |
19.06.2017 20:23 |
19.06.2017 |
from web service |
Server Time Zone |
20.06.2017 02:23 |
20.06.2017 |
19.06.2017 23:23 |
20.06.2017 |
And at the beginning:
Input Data |
what we see in Nav |
what stores in the database |
|||
Function is called |
Service Default Time Zone Setting |
DateTime Field |
Date Field |
DateTime Field |
Date Field |
from code |
UTC |
19.06.2017 0:23 |
19.06.2017 |
18.06.2017 21:23 |
19.06.2017 |
from web service |
UTC |
19.06.2017 3:23 |
19.06.2017 |
19.06.2017 00:23 |
19.06.2017 |
from code |
Server Time Zone |
19.06.2017 0:23 |
19.06.2017 |
18.06.2017 21:23 |
19.06.2017 |
from web service |
Server Time Zone |
19.06.2017 3:23 |
19.06.2017 |
19.06.2017 00:23 |
19.06.2017 |
So be careful and watch your watch :)
Comments
-
Hello Nikolay,
Thanks for this interesting input. But personally it does not surprise me as it is clearly mentioned in official NAV documentation on MSDN. Check here msdn.microsoft.com/.../dd355398(v=nav.90).aspx section "Web Services and Regional Settings"
*This post is locked for comments