17 November 2006

Google Earth KML Date Formatting

I am just building stuff using Google Earth, especially the new time stamped stuff. Very nice, really.

However GE expects dates in the format 2006-11-17 which isn't so bad in itself. It's AppleScript's fault that it is a pain to format dates other than the system's preset. You just do a

set dateString to (year of theDate) & "-" & (month of theDate as number) & "-" & (day of theDate)

and there you go. Nevertheless GE still choked on my formatted dates: 2006-9-5 can't be parsed by Google Earth. The leading 0 is mandatory!? WTF!?

Well here is the solution:
on format_date(theDate)
tell theDate
set y to year as string
set m to (month of theDate as number) as string
if (count of m) is 1 then set m to "0" & m
set d to day as string
if (count of d) is 1 then set d to "0" & d
end tell
return y & "-" & m & "-" & d
end format_date

1 comment:

bernd said...

Hallo Armin,
ich bin kein Applescript Experte, aber wenn ich mir das Datum in ISO 8601 formatieren muss, dann verwende ich folgenden Code:

(* date in ISO 8601, http://www.wikipedia.org/wiki/ISO_8601 *)
set dateString to ¬
(year of (current date)) & "-" & ¬
(text -2 thru -1 of ("0" & (month of (current date) as number))) & "-" & ¬
(text -2 thru -1 of ("0" & (day of (current date) as number))) ¬
as string

Im Vergleich zu Deinem Beispiel hat das den Vorteil, dass es keine if-Abfragen enthält und somit schneller zu sein verspricht.