Date_Time_Formatter.from_simple_pattern
Documentation
Creates a formatter from a simple date-time format pattern. Every letter in the pattern is interpreted as a pattern character as described in the table below. Any character that is not a letter in the pattern is treated as a literal character. If a sequence of letters needs to be put in as a literal, it can be escaped using single quotes. Use two single quotes in a row to represent a single quote in the result. As explained below, curly braces can have special meaning (see 'yy'); to enter a literal curly brace, put it inside a quoted literal. Pattern characters are interpreted case insensitively, with the exception of `M/m' and 'H/h'.
Examples
Parsing date/time values
Date_Time.parse "2021-10-12T12:34:56.789+0200" "yyyy-MM-dd'T'HH:mm:ss.fZ" == (Date_Time.new 2021 10 12 12 34 56 millisecond=789 zone=(Time_Zone.new hours=2))
Date.parse "Tue, 12 Oct 2021" "ddd, d MMM yyyy" == (Date.new 2021 10 12)
Date_Time.parse "12/10/2021 5:34 PM" "d/M/Y h:mm a" == (Date_Time.new 2021 10 12 17 34 00)
Note that the default locale may not support full-length day/month names, so you may need to set a specific locale for this to work.
Date.parse "Thursday, 1 October '98" (Date_Time_Formatter.from "dddd, d MMMM ''yy" Locale.uk) == (Date.new 1998 10 01)
Omitting the day will yield the first day of the month.
Date.parse "2021-10" "yyyy-MM" == (Date.new 2021 10 01)
Omitting the year will yield the current year.
Date.parse "10-12" "MM-dd" == (Date.new (Date.today.year) 10 12)
Parsing a two-digit year with a custom base year.
Date.parse "1 Nov '95" "d MMM ''yy{2099}" == (Date.new 2095 11 01)
Remarks
Date pattern characters
- 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.
- e: An alternative notation: single
e
maps toddd
andee
or more map todddd
meaning name of day of week. - 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.
Time pattern characters
- 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.
Time zone pattern characters
- 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.
- Z, ZZ, ZZZ: A short offset form (+HHmm).
No offset is indicated by "+0000". This can be customized by setting
an alternative no offset string in curly braces, e.g.
zz{Z}
. - ZZZZ: Localized offset (e.g. GMT-08:00).
- ZZZZZ: A full offset form (+HH:mm:ss).
No offset is indicated by "Z". This can be customized as above, e.g.
ZZZZZ{0}
.
- Z, ZZ, ZZZ: A short offset form (+HHmm).
No offset is indicated by "+0000". This can be customized by setting
an alternative no offset string in curly braces, e.g.
- v: Time zone name (same as TTTT).
- V: Time zone ID (same as T).
Some parts, like fractions of a second may not be required. The square
brackets []
can be used to surround such optional sections.