On this page:
15.1 Formatting Dates and Times
~t
15.2 Parsing Dates and Times
15.2.1 Parsing ISO 8601 representations
iso8601->date
iso8601->time
iso8601->datetime
iso8601->moment
iso8601/  tzid->moment
15.2.2 Flexible parsing based on patterns
parse-date
parse-time
parse-datetime
parse-moment
current-two-digit-year-resolver
7.7

15 Formatting and Parsing

15.1 Formatting Dates and Times

procedure

(~t t pattern [#:locale locale])  string?

  t : (or/c date-provider? time-provider?)
  pattern : string?
  locale : string? = (current-locale)
Formats t using the given pattern . The pattern syntax is specified by CLDR.

Examples:
> (parameterize ([current-locale "en"])
    (displayln (~t (date 1955 11 12) "E, MMMM d, y G"))
    (displayln (~t (datetime 1955 11 12 2 42 30) "E h:mm a")))

Sat, November 12, 1955 AD

Sat 2:42 AM

> (parameterize ([current-locale "fr"])
    (displayln (~t (date 1955 11 12) "E d MMM y G"))
    (displayln (~t (datetime 1955 11 12 2 42 30) "E h:mm:ss a")))

sam. 12 nov. 1955 ap. J.-C.

sam. 2:42:30 AM

15.2 Parsing Dates and Times

15.2.1 Parsing ISO 8601 representations

procedure

(iso8601->date str)  date?

  str : string?
Parses an ISO 8601 representation of a date into a date. Note that the input must use the ISO 8601 extended format.

Examples:
> (iso8601->date "1981-04-05")

#<date 1981-04-05>

> (iso8601->date "1981-04")

#<date 1981-04-01>

procedure

(iso8601->time str)  date?

  str : string?
Parses an ISO 8601 representation of a time into a time. Note that the input must use the ISO 8601 extended format.

Example:
> (iso8601->time "13:47:30")

#<time 13:47:30>

procedure

(iso8601->datetime str)  date?

  str : string?
Parses an ISO 8601 combined date and time representation into a datetime. Note that the input must use the ISO 8601 extended format.

Example:
> (iso8601->datetime "2014-03-20T19:20:09.3045")

#<datetime 2014-03-20T19:20:09.3045>

procedure

(iso8601->moment str)  date?

  str : string?
Parses an ISO 8601 combined date and time representation into a moment. Note that the input must use the ISO 8601 extended format.

Example:
> (iso8601->moment "2014-03-20T19:20:09.3045Z")

#<moment 2014-03-20T19:20:09.3045Z>

procedure

(iso8601/tzid->moment str)  date?

  str : string?
Parses a non-standard format, consisting of an ISO 8601 combined date and time representation and an IANA time zone ID in brackets, into a moment. The input format is the same as that produced by moment->iso8601/tzid. Note that the ISO 8601 portion of the input must use the ISO 8601 extended format.

Example:
> (iso8601/tzid->moment "2014-03-20T19:20:09.3045-04:00[America/New_York]")

#<moment 2014-03-20T19:20:09.3045-04:00[America/New_York]>

15.2.2 Flexible parsing based on patterns

procedure

(parse-date str    
  pattern    
  [#:ci? ci?    
  #:locale locale])  date?
  str : string?
  pattern : string?
  ci? : boolean? = #t
  locale : string? = (current-locale)
Parses str according to pattern, which uses the CLDR pattern syntax. The result is returned as a date. If the input cannot be parsed as a date, exn:gregor:parse is raised.

Example:
> (parameterize ([current-locale "en"])
    (list
     (parse-date "January 24, 1977" "LLLL d, y")
     (parse-date "2015-03-15T02:02:02-04:00" "yyyy-MM-dd'T'HH:mm:ssxxx")))

'(#<date 1977-01-24> #<date 2015-03-15>)

procedure

(parse-time str    
  pattern    
  [#:ci? ci?    
  #:locale locale])  time?
  str : string?
  pattern : string?
  ci? : boolean? = #t
  locale : string? = (current-locale)
Parses str according to pattern, which uses the CLDR pattern syntax. The result is returned as a time. If the input cannot be parsed as a time, exn:gregor:parse is raised.

Example:
> (parameterize ([current-locale "en"])
    (list
     (parse-time "January 24, 1977" "LLLL d, y")
     (parse-time "2015-03-15T02:02:02-04:00" "yyyy-MM-dd'T'HH:mm:ssxxx")))

'(#<time 00:00:00> #<time 02:02:02>)

procedure

(parse-datetime str    
  pattern    
  [#:ci? ci?    
  #:locale locale])  datetime?
  str : string?
  pattern : string?
  ci? : boolean? = #t
  locale : string? = (current-locale)
Parses str according to pattern, which uses the CLDR pattern syntax. The result is returned as a datetime. If the input cannot be parsed as a datetime, exn:gregor:parse is raised.

Example:
> (parameterize ([current-locale "en"])
    (list
     (parse-datetime "January 24, 1977" "LLLL d, y")
     (parse-datetime "2015-03-15T02:02:02-04:00" "yyyy-MM-dd'T'HH:mm:ssxxx")))

'(#<datetime 1977-01-24T00:00:00> #<datetime 2015-03-15T02:02:02>)

procedure

(parse-moment str    
  pattern    
  [#:ci? ci?    
  #:locale locale]    
  #:resolve-offset resolve)  moment?
  str : string?
  pattern : string?
  ci? : boolean? = #t
  locale : string? = (current-locale)
  resolve : resolve-offset/raise
Parses str according to pattern, which uses the CLDR pattern syntax. The result is returned as a moment. If the input cannot be parsed as a moment, exn:gregor:parse is raised. If the result’s UTC offset is ambigous, resolve is used to resolve the ambiguity.

Example:
> (parameterize ([current-locale "en"]
                 [current-timezone "Pacific/Honolulu"])
    (list
     (parse-moment "January 24, 1977" "LLLL d, y")
     (parse-moment "2015-03-15T02:02:02-04:00" "yyyy-MM-dd'T'HH:mm:ssxxx")))

'(#<moment 1977-01-24T00:00:00-10:00[Pacific/Honolulu]>

  #<moment 2015-03-15T02:02:02-04:00>)

parameter

(current-two-digit-year-resolver)

  (-> (integer-in 0 99) exact-integer?)
(current-two-digit-year-resolver resolver)  void?
  resolver : (-> (integer-in 0 99) exact-integer?)
A parameter used to control how parsed two-digit years are resolved into complete years. The default implementation is:
(λ (parsed-year)
  (define current-year (->year (now)))
  (define lo (- current-year 50))
  (define t (if (>= lo 0)
                (remainder lo 100)
                (+ 99 (remainder (add1 lo) 100))))
 
  (+ parsed-year
     lo
     (if (< parsed-year t)
         100
         0)
     (- t)))

Example:
> (parameterize ([current-two-digit-year-resolver (λ (y) (+ 3300 y))])
    (parse-date "3/3/33" "M/d/yy"))

#<date 3333-03-03>