To_datetime

Converts a string to a standardized datetime object.

Syntax

datetime to_datetime(string $data, string $format, string $culture)

Converts $data string, formatted as defined in $format to default date/time by $culture.

Parameters

$data: The input string.

$format: Definition of the input string. This is expected as a datetime format string to use for parsing.

$culture: The desired date/time output format. This should be a language culture name, based on ISO-639-1 (ie. ‘en-US‘, ‘es-ES‘).

Example

{
  "startDate": "2021-02-21 12:00:00Z",
  "endDate": "3 March, 2021"
}
{
  formattedStartDate:to_datetime(startDate, 'u', 'en-US'),
  formattedEndDate:to_datetime(endDate, 'd MMMM, yyyy', 'en-US')
}
{
  "formattedStartDate": "2021-02-21T12:00:00",
  "formattedEndDate": "2021-03-03T00:00:00"
}

In this example the start date and end dates are formatted differently. startDate conforms to the universal sortable datetime format so ‘u’ is defined as $format. endDate is formatted as d MMMM, yyyy.

Notes

This function is often used in conjunction with the format function to convert any input date format to any output date format.