# format_datetime



The `format_datetime` function formats a `datetime` value as a string according to a format pattern.

## Usage [#usage]

### Syntax [#syntax]

```kusto
format_datetime(date, format)
```

### Parameters [#parameters]

| Parameter | Type       | Description                                                                                                    |
| --------- | ---------- | -------------------------------------------------------------------------------------------------------------- |
| `date`    | `datetime` | The datetime value to format.                                                                                  |
| `format`  | `string`   | A non-empty string literal specifying the output format. Dynamic expressions or empty strings return an error. |

### Supported format specifiers [#supported-format-specifiers]

Other characters in `format` are copied as-is. Month and day names are not supported.

| Specifier         | Description                                                   |
| ----------------- | ------------------------------------------------------------- |
| `y`, `yy`, `yyyy` | Year (2-digit or 4-digit).                                    |
| `M`, `MM`         | Month number (`1`–`12` or `01`–`12`).                         |
| `d`, `dd`         | Day of month (`1`–`31` or `01`–`31`).                         |
| `H`, `HH`         | 24-hour clock (`0`–`23` or `00`–`23`).                        |
| `h`, `hh`         | 12-hour clock (`1`–`12` or `01`–`12`).                        |
| `m`, `mm`         | Minutes (`0`–`59` or `00`–`59`).                              |
| `s`, `ss`         | Seconds (`0`–`59` or `00`–`59`).                              |
| `tt`              | AM/PM designator.                                             |
| `f`–`fffffff`     | Fractional seconds (1 to 7 digits, including trailing zeros). |
| `F`–`FFFFFFF`     | Fractional seconds (1 to 7 digits, trailing zeros omitted).   |

### Returns [#returns]

Returns a formatted `string` representation of `date`.

## Examples [#examples]

**Query**

```kusto
let dt = datetime(2017-01-29 09:00:05);
print
    fmt1 = format_datetime(dt, 'yy-MM-dd [hh:mm:ss tt]'),
    fmt2 = format_datetime(dt, 'yyyy-M-d H:mm:ss.fff'),
    fmt3 = format_datetime(dt, 'dd/MM/yyyy HH:mm:ss.FFF')
```

**Output**

| fmt1                     | fmt2                    | fmt3                   |
| ------------------------ | ----------------------- | ---------------------- |
| `17-01-29 [09:00:05 AM]` | `2017-1-29 9:00:05.000` | `29/01/2017 09:00:05.` |

## List of related functions [#list-of-related-functions]

* [**datetime\_part**](/apl/scalar-functions/datetime-functions/datetime-part): Extracts an integer component from a datetime value.
* [**datetime\_add**](/apl/scalar-functions/datetime-functions/datetime-add): Adds a period to a datetime value.
* [**datetime\_diff**](/apl/scalar-functions/datetime-functions/datetime-diff): Calculates the difference between two datetime values.
