Text.parse_date
parse_dateformat
Group: Conversions
Aliases: date from text
, to_date
Documentation
Converts text containing a date into a Date object. 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 the date of 23rd December 2020.
import Standard.Base.Data.Text.Extensions
example_parse = "2020-12-23".parse_date
Recover from an error due to a wrong format.
import Standard.Base.Data.Text.Extensions
from Standard.Base.Errors.Common import Time_Error
example_parse_err = "my birthday".parse_date . catch Time_Error _->
Date.new 2000 1 1
Parse "1999-1-1" as Date using a custom format.
import Standard.Base.Data.Text.Extensions
example_parse = "1999-1-1".parse_date "yyyy-M-d"
Recover from the parse error.
import Standard.Base.Data.Text.Extensions
from Standard.Base.Errors.Common import Time_Error
example_parse_err =
date = "1999-1-1".parse_date "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.