#datetime
DateTimeCreates a datetime value from year, month, day, hour, minute, and second components.
Syntax
#datetime(year as number, month as number, day as number, hour as number, minute as number, second as number) as datetimeParameters
| Name | Type | Required | Description |
|---|---|---|---|
year | number | Yes | The year component (e.g. 2024). |
month | number | Yes | The month component (1–12). |
day | number | Yes | The day component (1–31, depending on month). |
hour | number | Yes | The hour component (0–23). |
minute | number | Yes | The minute component (0–59). |
second | number | Yes | The second component (0–59, can include fractional seconds). |
Return Value
datetime — A datetime value representing the specified date and time.
Remarks
#datetime is M's literal constructor for creating datetime values — a combined date and time with no timezone offset.
The second argument may include fractional seconds (e.g. 30.5 for 30.5 seconds). Out-of-range component values produce an error.
#datetime does not encode a timezone. If you need to represent a specific moment in time relative to UTC, use #datetimezone instead. To strip the time component from a datetime, use DateTime.Date. To extract the time component, use DateTime.Time.
Examples
Example 1: Create a datetime literal
#datetime(2024, 3, 15, 14, 30, 0)Result
Result | |
|---|---|
| 1 | 3/15/2024 2:30:00 PM |
Example 2: Extract date and time parts
let
dt = #datetime(2024, 3, 15, 14, 30, 0),
d = DateTime.Date(dt),
t = DateTime.Time(dt)
in
{d, t}Result
Result | |
|---|---|
| 1 | {2024-03-15, 14:30:00} |
Example 3: Subtract two datetimes to get a duration
let
start = #datetime(2024, 3, 15, 9, 0, 0),
finish = #datetime(2024, 3, 15, 17, 30, 0),
elapsed = finish - start
in
elapsedResult
Result | |
|---|---|
| 1 | PT8H30M |
Compatibility
✓ Power BI Desktop✓ Power BI Service✓ Excel Desktop✓ Excel Online✓ Dataflows✓ Fabric Notebooks