DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Parse-simple-date Date String Parser
parse-simple-date: func [
"Parse a string containing a date value; return a date! value."
date [any-string!]
/def-day def-d [integer! word!] "Default day for mm/yyyy format. Number or 'last."
/local dig sep d m y set-def-day tmp-dt
][
dig: charset [#"0" - #"9"]
sep: charset " -/."
set [d m y] none
set-def-day: does [
d: any [
all [integer? def-d def-d]
all [
'last = def-d
foreach fld [d m y] [set fld to integer! get fld]
tmp-dt: subtract make date! reduce [1 m + 1 y] 1
tmp-dt/day
]
1
]
]
; assuming mm/dd/yy or mm/yy format
either parse/all date [
[copy m 1 2 dig sep copy d 1 2 dig sep copy y 1 4 dig]
| [copy m 1 2 dig sep copy y 1 4 dig (set-def-day)]
][
foreach fld [d m y] [set fld to integer! get fld]
; add century if necessary; window from 1926-2025
if y < 100 [y: add y pick [1900 2000] y > 25]
; swap day and month if it makes sense
if all [m > 12 d <= 12] [set [m d] reduce [d m]]
make date! reduce [d m y]
][none]
]




