Date.parse
Date.parsetextformat
Group: Conversions
Aliases: date from text
Documentation
Converts text containing a date into a Date object.
Returns a Time_Error
if the provided text
cannot be parsed using the provided format
.
Arguments
text
: The text to try and parse as a date.format
: A pattern describing how to parse the text, or aDate_Time_Formatter
.
Examples
Parse the date of 23rd December 2020.
from Standard.Base import Date
example_parse = Date.parse "2020-12-23"
Recover from an error due to a wrong format.
from Standard.Base import Date
from Standard.Base.Errors.Common import Time_Error
example_parse_err = Date.parse "my birthday" . catch Time_Error _->
Date.new 2000 1 1
Parse "1999-1-1" as Date using a custom format.
from Standard.Base import Date
example_parse = Date.parse "1999-1-1" "yyyy-M-d"
Recover from the parse error.
from Standard.Base import Date
from Standard.Base.Errors.Common import Time_Error
example_parse_err =
date = Date.parse "1999-1-1" "yyyy-MM-dd"
date.catch Time_Error (_->Date.new 2000 1 1)
Remarks
Default Date Formatting
Unless you provide a custom format, the text must represent a valid date that can be parsed using the ISO-8601 extended local date format. The format consists of:
- Four digits or more for the year. Years in the range 0000 to 9999 will be pre-padded by zero to ensure four digits. Years outside that range will have a prefixed positive or negative symbol.
- A dash
- Two digits for the month-of-year. This is pre-padded by zero to ensure two digits.
- A dash
- Two digits for the day-of-month. This is pre-padded by zero to ensure two digits.
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.