AppleScript Date to String

Wenn man mal NUR ein AppleScript-Date zur Verfügung hat, daraus aber einen normalen Datums-String bauen muss hier eine Funktion dazu:

Code zum markieren einmal anklicken Code im Skript-Editor öffnen

AppleScriptDateToString(current date)

on AppleScriptDateToString(a)

set b to current date

set monthnames to {}

repeat with i from 1 to 12

set month of b to i

set monthnames to monthnames & {(month of b) as text}

end repeat

set Y to (year of b)

set M to 0

repeat with t in monthnames

set M to M + 1

if t as text = (month of b) as text then

exit repeat

end if

end repeat

set M to Twodigits(M)

set D to Twodigits(day of b)

set hh to Twodigits(hours of b)

set mm to (minutes of b)

set ss to Twodigits(seconds of b)

–“2009-12-17 01:26:49”

return Y & “-” & M & “-” & D & ” ” & hh & “:” & mm & “:” & ss as text

–“2009-12-17 01:28”

–return Y & “-” & M & “-” & D & ” ” & hh & “:” & mm as text

–“2009-12-17”

–return Y & “-” & M & “-” & D as text

end AppleScriptDateToString

on Twodigits(a)

return (characters -2 through -1 of ((“0” & a) as text)) as text

end Twodigits

Wenn man nur das aktuelle Datum braucht, dürfte date etwas einfach und schneller sein

Code zum markieren einmal anklicken

set current_date_string to do shell script “date ‘+%Y-%m-%d %H:%M:%S'”