Hi,
Tired of having so many dateTime conversion utilities I recently wrote a "function to bind 'em all".
The idea is to trow it a dateTime string of whatever format and have it decide itself what it is, and to convert it to dateItems at first.
DateItems because it's the most easy way to calculate with dateTimes - add/ subtract days here.
And then have it output a nicely formatted dateTime, depending on the provided format designator - or empty, if no date could be recognized.
Since there's so many date formats so differently formatted ("MM/DD/YY", "DD.MM.YYYY", "YYYY-MM-DD", to show only a few numeric-only examples), and since I'm lazy, and since I my programs are only used in the german speaking part of the world, my "format-guessing" expects these formats: "DD.MM.YYYY hh:mm" and "DD. MonthName YYYY, hh:mm" for system dateTimes. A comment containing "###" designates these cases. Else I'm heavily counting on "system date" in converting.
This function seems to be quite fast & reliable, but isn't extensively tested yet. Be warned, there may be quirks!
I'm posting it here because it shows quite some ways to handle dateTimes, and it may be helpful for someone.
Should run on any version of LC, for non-german systems adaptions will be required :)
Have fun!
Call: put formatDate("2018-6-29 13:34","SSYD",2)
Result: 01.07.18
XPlain: Give me the short system date (2digit year) of the DateTimeString (a SQL dateTime), with 2 days added
Code: Select all
function formatDate what, theTarget, dayAdd
-- what: Str - any date string
-- theTarget: Str - the desired format to return, look up the second switch structure
-- dayAdd: SigInt - days to add or subtract (negative values)
if theTarget is empty then put "SYSD" into theTarget -- default
-- at first: cleanup:
repeat
if char -1 of what = " " then -- kill trailing spaces
delete char -1 of what
else exit repeat
end repeat
replace "," with " " in what -- kill commata
repeat -- kill double spaces
if " " is in what then
replace " " with " " in what
else exit repeat
end repeat
-- then decide what we have:
put empty into myVar
repeat for each char C in what
if C is a number then put "N" after myVar -- (Numeric)
if C = "." then put "P" after myVar -- (Point)
if C = "-" then put "M" after myVar -- (Minus)
if C = " " then put "S" after myVar -- (Space)
if C = ":" then put "D" after myVar -- (Double Dot)
if (charToNum(C) > 64 AND charToNum(C) < 91) OR (charToNum(C) > 96 AND charToNum(C) < 123) then put "C" after myVar -- (Char)
end repeat
-- myVar can be analyzed now:
switch
case ("N" is in myVar) AND ("P" is in myVar) AND ("C" is in myVar) AND NOT ("D" is in myVar)
put "LNGD" into myFormat -- 13. May 2018 -- ### Long Date, SYSTEM SPECIFIC
convert what from long system date to dateitems
break
case ("N" is in myVar) AND ("P" is in myVar) AND ("C" is in myVar) AND ("S" is in myVar) AND ("D" is in myVar)
put "LNGT" into myFormat -- 13. May 2018 13:45 -- ### Long DateTime, SYSTEM SPECIFIC
convert what from long system date and system time to dateitems
break
case ("N" is in myVar) AND ("P" is in myVar) AND NOT ("C" is in myVar) AND NOT ("D" is in myVar)
put "SYSD" into myFormat -- 13.7.2018 -- ### Short Date, SYSTEM SPECIFIC
convert what from system date to dateitems
break
case ("N" is in myVar) AND ("P" is in myVar) AND NOT ("C" is in myVar) AND ("S" is in myVar) AND ("D" is in myVar)
put "SYST" into myFormat -- 13.7.2018 13:45 -- ### Short DateTime, SYSTEM SPECIFIC
convert what from system date and system time to dateitems
break
case ("N" is in myVar) AND ("M" is in myVar) AND NOT ("D" is in myVar)
put "SQLD" into myFormat -- 2018-07-13 -- SQL Date
set itemdel to "-"
put item 3 of what & "." & item 2 of what & "." & item 1 of what into what
convert what from system date to dateitems
break
case ("N" is in myVar) AND ("M" is in myVar) AND ("S" is in myVar) AND ("D" is in myVar)
put "SQLT" into myFormat -- 2018-07-13 13:45 -- SQL DateTime
set itemdel to space
put item 1 of what into myVar
set itemdel to "-"
put item 3 of myVar & "." & item 2 of myVar & "." & item 1 of myVar into myVar
set itemdel to space
put myVar into item 1 of what
convert what from system date and system time to dateitems
break
default
put empty into myFormat -- couldn't resolve
end switch
if myFormat is empty then return empty -- cannot find a date that I know :/
-- what is now dateItems - maybe we must add some days?
if dayAdd is not empty then
set itemdel to comma
add dayadd to item 3 of what
end if
-- formatting the output:
switch theTarget
case "LNGD"
convert what from dateItems to long system date
break
case "LNGT"
convert what from dateItems to long system date and system time
break
case "SYSD"
convert what from dateItems to system date
break
case "SYST"
convert what from dateItems to system date and system time
break
case "SSYD" -- Short (Short System Date) - 2digit year
convert what from dateItems to system date
set itemdel to "."
delete char 1 to 2 of item 3 of what
break
case "SSYT" -- Short (Short System DateTime) - 2digit year
convert what from dateItems to system date and system time
set itemdel to "."
delete char 1 to 2 of item 3 of what
break
case "SQLD"
convert what from dateItems to dateitems
set itemdel to comma
put item 1 of what & "-" & format("%02s", item 2 of what) & "-" & format("%02s", item 3 of what) into what
break
case "SQLT"
convert what from dateItems to dateitems
set itemdel to comma
put item 1 of what & "-" & item 2 of what & "-" & item 3 of what & \
" " & item 4 of what & ":" & item 5 of what into what
break
default
return what -- shouldn't happen ...
end switch
return what -- wOOOt!
end formatDate