Skip to main content

Text.parse_date_time

parse_date_timeformat

Group: Conversions
Aliases: date_time from text

Documentation

Obtains an instance of Date_Time from a text such as "2007-12-03T10:15:30+01:00 Europe/Paris". This method will return a Time_Error if the provided time cannot be parsed.

Arguments

  • format: The format to use for parsing the input text.

Examples

Parse UTC time.

      import Standard.Base.Data.Text.Extensions

example_parse = "2020-10-01T04:11:12Z".parse_date_time

Parse UTC-04:00 time.

      import Standard.Base.Data.Text.Extensions

example_parse = "2020-10-01T04:11:12-04:00".parse_date_time

Parse UTC-04:00 time specifying New York timezone.

      import Standard.Base.Data.Text.Extensions

example_parse = "2020-10-01T04:11:12-04:00[America/New_York]".parse_date_time

Parse UTC-04:00 time with nanoseconds.

      import Standard.Base.Data.Text.Extensions

example_parse = "2020-10-01T04:11:12.177528-04:00".parse_date_time

Recover from the parse error.

      import Standard.Base.Data.Text.Extensions

example_parse = "2020-10-01".parse_date_time . catch Time_Error (_->Date_Time.now)

Parse "2020-05-06 04:30:20" as Date_Time

      import Standard.Base.Data.Text.Extensions

example_parse = "2020-05-06 04:30:20".parse_date_time "yyyy-MM-dd HH:mm:ss"

Parse "06 of May 2020 at 04:30AM" as Date_Tme

      import Standard.Base.Data.Text.Extensions

example_parse =
"06 of May 2020 at 04:30AM".parse_date_time "dd 'of' MMMM yyyy 'at' hh:mma"

Remarks

Default Date_Time Format

Unless you provide a custom format, the text must represent a valid date-time as defined by the ISO-8601 format (see https://en.wikipedia.org/wiki/ISO_8601). If a time zone is present, it must be in the ISO-8601 Extended Date/Time Format (EDTF) (see https://en.wikipedia.org/wiki/ISO_8601#EDTF). The time zone format consists of:

  • The ISO offset date time.
  • If the zone ID is not available or is a zone offset then the format is complete.
  • An open square bracket '['.
  • The zone ID. This is not part of the ISO-8601 standard. Parsing is case sensitive.
  • A close square bracket ']'.

Pattern Syntax

If the pattern is provided as Text, it is parsed using the format described below. See Date_Time_Formatter for more options.

  • y: Year. The number of pattern letters determines the minimum number of digits.
    • y: The year using any number of digits.
    • yy: The year, using at most two digits. The default range is 1950-2049, but this can be changed by including the end year in braces e.g. yy{2099}.
    • yyyy: The year, using exactly four digits.
  • M: Month of year. The number of pattern letters determines the format:
    • M: Any number (1-12).
    • MM: Month number with zero padding required (01-12).
    • MMM: Short name of the month (Jan-Dec).
    • MMMM: Full name of the month (January-December). The month names depend on the selected locale.
  • d: Day. The number of pattern letters determines the format:
    • d: Any number (1-31).
    • dd: Day number with zero padding required (01-31).
    • ddd: Short name of the day of week (Mon-Sun).
    • dddd: Full name of the day of week (Monday-Sunday). The weekday names depend on the selected locale. Both day of week and day of month may be included in a single pattern - in such case the day of week is used as a sanity check.
  • Q: Quarter of year. If only year and quarter are provided in the pattern, when parsing a date, the result will be the first day of that quarter.
  • H: 24h hour of day (0-23).
  • h: 12h hour of day (0-12). The a pattern is needed to disambiguate between AM and PM.
  • m: Minute of hour.
  • s: Second of minute.
  • f: Fractional part of the second. The number of pattern letters determines the number of digits. If one letter is used, any number of digits will be accepted.
  • a: AM/PM marker.
  • T: If repeated 3 or less times - Time zone ID (e.g. Europe/Warsaw, Z, -08:30), otherwise - Time zone name (e.g. Central European Time, CET).
  • Z: Zone offset (e.g. +0000, -0830, +08:30:15).