Dynamics 365 Business Central: Date, Time, DateTime Conversions (Date Type -> DateTime Type, DateTime Type -> Date Type, DateTime Type -> Time Type)

Dynamics 365 Business Central

Hi, Readers.
Today I would like to briefly talk about Date, Time, DataTime conversions in Business Central, for example, how to quickly convert Date Data Type to DateTime Data Type.

This is a question that I see every once in a while in Dynamics 365 community. I have even seen some developers use methods for processing text to split and merge values.
It’s not really that difficult, so let’s read on.

First let’s look at the definitions of these three date types.

Date Data Type: Denotes a date ranging from January 1, 1753 to December 31, 9999.

Time Data Type: Denotes a time ranging from 00:00:00.000 to 23:59:59.999. An undefined or blank time is specified by 0T.

DateTime Data Type: Denotes a date and time ranging from January 1, 1753, 00:00:00.000 to December 31, 9999, 23:59:59.999.

We have discussed how to use Date and Time Functions before, and this time we need to use some methods in it.

1. Date Data Type -> DateTime Data Type

We can use System.CreateDateTime(Date, Time) Method.

System.CreateDateTime(Date, Time) Method: Creates a DateTime object from a date and a time.

Syntax: Datetime := System.CreateDateTime(Date: Date, Time: Time)

For example,

Or,

2. DateTime Data Type -> Date Data Type

We can use System.DT2Date(DateTime) Method.
System.DT2Date(DateTime) Method: Gets the date part of a DateTime object.

Syntax: Date := System.DT2Date(Datetime: DateTime)

For example,

3. DateTime Data Type -> Time Data Type

System.DT2Time(DateTime) Method: Gets the time part of a DateTime object.

Syntax: Time := System.DT2Time(Datetime: DateTime)

For example,

Very easy, isn’t it? Give it a try!!!😁

More info:
How to use Date and Time Functions
Working with Duration Data Type
How to quickly get the day of the week from a date (Date virtual table)
How to quickly get Coordinated Universal Time (UTC Time)

Update 2024.09.05: Business Central 2024 wave 2 (BC25): New Date/Time/DateTime methods

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL