How to convert Date to DateTime in Business Central [AL- VsCode]?

I have to assign a "Date" Value to a "DateTime" variable. How do I convert the "Date" to a "DateTime"?

There are two ways to achieve this,
  1. Using CREATEDATETIME procedure
  2. Using EVALUATE procedure

Using CREATDATETIME Procedure:

Use procedure CREATEDATETIME where your date is first parameter and 0T is second parameter.
YourDateTime := CREATEDATETIME(YourDate,0T);

I would suggest to create a procedure where parameter is your Date to convert and return value is your datetime variable.


You can copy paste following:

   
    /// <summary>
    /// ConvertDateToDateTime using CREATEDATETIME
    /// </summary>
    /// <param name="YourDate">Date.</param>
    /// <returns>Return variable YourDateTime of type DateTime.</returns>
    procedure ConvertDateToDateTime(YourDate: Date) YourDateTime: DateTime
    begin
        //using CREATEDATETIME
        YourDateTime := CreateDateTime(YourDate, 0T);
       
    end;


Using EVALUATE procedure:

Use procedure EVALUATE where your datetime variable is first parameter and yourdate inside Format is second parameter.
Evaluate(YourDateTime, Format(YourDate));

I would suggest to create a procedure where parameter is your Date to convert and return value is your datetime variable.


If you want to copy the code:

   
    /// <summary>
    /// ConvertDateToDateTime using EVALUATE
    /// </summary>
    /// <param name="YourDate">Date.</param>
    /// <returns>Return variable YourDateTime of type DateTime.</returns>
    procedure ConvertDateToDateTime(YourDate: Date) YourDateTime: DateTime
    begin
        //USING EVALUATE
        Evaluate(YourDateTime, Format(YourDate));
    end;



Conclusion:
These are two ways I know to convert date to DateTime in business central and NAV. I hope you found this useful, thank you for reading.

Post a Comment

Thank you for comment, I really appreciate your view.

–>