to_datetime

Convert a string to a datetime object

Definition

date to_datetime(string $text, string $format, string $culture)

Converts a string representation of a moment in time $text into a datetime object. $format and $culture are required to parse the string correctly.

Parameters

string $text

The text representing a date or a time.

string $format

The format of the provided string. We use Microsoft's standard for .NET to describe the format of the provided string.

string $culture

A culture specifier, a combination of a language and region tag.

Returns

datetime

A datetime representation of $text

Examples

{
  a: to_datetime('2021-02-21 12:00:00Z', 'u', 'en-US'),
  b: to_datetime('3 Feb, 2021', 'd MMM, yyyy', 'en-US'),
  c: to_datetime('3 February, 2021', 'd MMMM, yyyy', 'en-US'),
  d: to_datetime('', 'd MMMM, yyyy', 'en-US'),
  e: to_datetime(`null`, 'd MMMM, yyyy', 'en-US'),
  f: to_datetime('12:00', 'HH:mm', 'da-DK'),
  g: to_datetime('23', 'dd', 'en-US'),
  h: to_datetime('23', 'yy', 'en-US')
}
{
  "a": "2021-02-21T12:00:00",
  "b": "2021-02-03T00:00:00",
  "c": "2021-02-03T00:00:00",
  "d": null,
  "e": null,
  "f": "2021-08-15T12:00:00",
  "g": "2021-01-23T00:00:00",
  "h": "2023-01-01T00:00:00"
}

Remarks

The default values, if only some time units are specified, are:

  • Day: 01
  • Month: 01
  • Year: Current year
  • Other units: 00
  • If time of day is the only thing specified, the return date is the current date.