Today, we had an issue that a web service returned a timestamp in unixtime format, ie. all seconds that have passed since January 1st 1970.

We would like to convert that timestamp to a DateTime variable in AL Code.

We finally came up with these functions: UnixTimeToDateTime (see https://github.com/TBits/Al-examples/blob/master/ALBasics/cod50101.AlBasics.al#L8) and DateTimeToUnixTime (see https://github.com/TBits/Al-examples/blob/master/ALBasics/cod50101.AlBasics.al#L36).

See also the example calls at https://github.com/TBits/Al-examples/blob/master/ALBasics/pag50101.ALBasics.al

procedure TestUnixTime();
var
	myCodeUnit: Codeunit myLibrary;
	nowUnixTime: BigInteger;
	FormatString: Text;
	nowDateTime: DateTime;
begin
	nowUnixTime := myCodeUnit.DateTimeToUnixTime(CurrentDateTime);
	Message('to unixtime: ' + Format(nowUnixTime));
	nowDateTime := myCodeUnit.UnixTimeToDateTime(nowUnixTime);
	// formatting the date time to string
	FormatString := '<Day,2>.<Month,2>.<Year4> <Hours24,2>:<Minutes,2>:<Seconds,2>.<Thousands,3>';
	Message('from unixtime: ' + Format(nowDateTime, 0, FormatString));
end;

Also note how you can specify the formatting of a date with the keywords Day, Month, Year4, Hours24, Minutes.

AL Coding: Converting DateTime and UnixTime timestamps in Dynamics NAV 2018
Tagged on: