Get current UTC time in AL

idonav
Member Posts: 31
Hi all,
I would like to find a way to get the current UTC date in AL. I've been messing around with DateTime and similar, but it seems like it takes the user agent time zone in to account (which is out of our control), and it all just becomes a big mess with DateTimes not making any sense.
So, I need a way to get current UTC time, it is not an option to do a get request to external services, neither tampering with client computer time zones (spread around the world). All servers have their timezone set to UTC, so maybe I could fetch it from there in some way?
Suggestions appreciated, seems like a super simple task, but for some reason I'm not getting it to work.
I would like to find a way to get the current UTC date in AL. I've been messing around with DateTime and similar, but it seems like it takes the user agent time zone in to account (which is out of our control), and it all just becomes a big mess with DateTimes not making any sense.
So, I need a way to get current UTC time, it is not an option to do a get request to external services, neither tampering with client computer time zones (spread around the world). All servers have their timezone set to UTC, so maybe I could fetch it from there in some way?
Suggestions appreciated, seems like a super simple task, but for some reason I'm not getting it to work.
0
Best Answer
-
And you can use thispageextension 50100 CustomerListExt extends "Customer List" { trigger OnOpenPage(); var DateTime: DateTime; begin DateTime := CurrentDateTime(); Message('UTC Date: %1', Format(DateTime, 0, 9)); end; }
The fuction Format(DateTime, 0, 9) give you an string with the date and time in UTC5
Answers
-
You can use a default .net object that get that system data (you don't have to develop it) and put that in run on client = false; that should work0
-
DateTime.UtcNow from mscorlib would probably work if I developed a C/AL solution, but I need to do it in AL.0
-
In BC on-premise you can interop whit dotNET from AL, I don't know if that is your case.
This is the link:
https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-get-started-call-dotnet-from-al0 -
And you can use thispageextension 50100 CustomerListExt extends "Customer List" { trigger OnOpenPage(); var DateTime: DateTime; begin DateTime := CurrentDateTime(); Message('UTC Date: %1', Format(DateTime, 0, 9)); end; }
The fuction Format(DateTime, 0, 9) give you an string with the date and time in UTC5 -
Thank you @ftornero, that works perfectly fine. The problem described in the initial post only occurs when saving it to a datetime variable, but saving it as a string does the job.
Full code below, for other (not related to this) reasons, I had to convert it to a BigInt:procedure GetCurrentUtcTime() UtcTime: BigInteger; var DateTime: DateTime; TempTime: Text; begin DateTime := CurrentDateTime(); TempTime := Format(DateTime, 0, 9); TempTime := DelChr(TempTime, '=', '-:.TZ'); Evaluate(UtcTime, TempTime); end;
0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions