Zum Inhalt springen
Unix-Zeitstempel umwandeln in AL

From Epoch to Now: Deciphering the Magic Behind Unix Timestamps in AL!

Daniel Gorski
Daniel Gorski 09. Oktober 2023
2 Min. Lesezeit

Welcome to another exciting journey into the world of AL programming! Today, we'll demystify the enigma behind Unix timestamps and learn how to seamlessly convert between these number-heavy representations and the more friendly date and time formats we're familiar with.


Imagine being handed a number — 1696867491. Looks confusing, right? But did you know that this is actually a timestamp representing a specific moment in time? That's the beauty of Unix timestamps: their ability to capture moments as simple integers. And with AL, converting these cryptic numbers to human-friendly dates becomes a walk in the park!


🧩 From Timestamp to Date

Let’s dive into the UnixTimestampToDate procedure:

procedure UnixTimestampToDate(DateInMs: BigInteger): Date
var
    TypeHelper: Codeunit "Type Helper";
begin
    exit(DT2Date(TypeHelper.EvaluateUnixTimestamp(DateInMs)));
end;

This snippet effortlessly takes a Unix timestamp in milliseconds and, with the help of our trusty Type Helper, translates it into a format more pleasing to our eyes—a classic date.


🔁 From DateTime to Unix Timestamp

On the flip side, have you ever wondered how to send a date into the vast universe of Unix numbers? Our next hero, DateTimeToUnixTimestamp, does just that:

procedure DateTimeToUnixTimestamp(DateTimeValue: DateTime): BigInteger
var
    EpochDateTime: DateTime;
begin
    // Calculate the Unix timestamp based on the Epoch datetime of 1/1/1970
    EpochDateTime := CreateDateTime(DMY2Date(1, 1, 1970), 0T);
    exit((DateTimeValue - EpochDateTime) / 1000);
end;

By referencing the "starting point" of Unix time (the Epoch: midnight of January 1, 1970), this procedure efficiently calculates the seconds that have elapsed since, producing our desired Unix timestamp.


In essence, Unix timestamps and human-friendly dates are two sides of the same coin. And with AL, moving between these two realms has never been easier. So, the next time you come across a seemingly random number in your database or API calls, remember: there's a moment in time hidden inside, just waiting to be revealed! 📅🕰️


Stay tuned for more AL adventures, and happy coding!

beyondit
tech
Business Central
AL
DateTime
Unix Timestamp
How-To
E-Invoicing-Formate UBL, CII, ZUGFeRD und Factur-X im Vergleich

UBL, CII, ZUGFeRD, Factur-X: Die E-Invoicing-Formate endlich verständlich erklärt

UBL, CII, ZUGFeRD oder Factur-X – diese E-Invoicing-Formate werden ständig verwechselt. Wir erklären den Unterschied zwischen reinen XML-Formaten und Hybridform

Weiter lesen: UBL, CII, ZUGFeRD, Factur-X: Die E-Invoicing-Formate endlich verständlich erklärt
Weiter lesen: UBL, CII, ZUGFeRD, Factur-X: Die E-Invoicing-Formate endlich verständlich erklärt
E-Invoicing formats UBL, CII, ZUGFeRD and Factur-X compared

UBL, CII, ZUGFeRD, Factur-X: E-Invoicing Formats Finally Explained

UBL, CII, ZUGFeRD or Factur-X – these e-invoicing formats get confused all the time. We break down the difference between pure XML formats and hybrid PDF-plus-X

Weiter lesen: UBL, CII, ZUGFeRD, Factur-X: E-Invoicing Formats Finally Explained
Weiter lesen: UBL, CII, ZUGFeRD, Factur-X: E-Invoicing Formats Finally Explained