Need to change short date format of m/dd/yyyy to yyyy-mm-dd

So I have the following script that, thanks to several VERY nice folks here, allows me to grab all my gigs from my Palm Desktop calendar for any given month.

to FixDate(b)
	set new_Date to {}
	repeat with c from 1 to 2
		if (b's word c) < 10 then
			set end of new_Date to ("0" & (b's word c) & "/")
		else
			set end of new_Date to ((b's word c) & "/")
		end if
	end repeat
	set end of new_Date to "20" & (b's word 3)
	return (new_Date as string)
end FixDate

on sortEvents(a, l, r)
	script o
		property p : a
	end script
	using terms from application "Palm Desktop"
		repeat with i from l + 1 to r
			set thisEvent to o's p's item i
			set thisTime to thisEvent's start time
			repeat with j from i to l by -1
				if (j > l) and (thisTime comes before o's p's item (j - 1)'s start time) then
					set o's p's item j to o's p's item (j - 1)
				else
					set o's p's item j to thisEvent
					exit repeat
				end if
			end repeat
		end repeat
	end using terms from
end sortEvents

on monthRange(m, y)
	tell {(date (m & " 1 " & y)) - 1}
		set end to beginning + 32 * days + 1
		set end's day to 1
		it
	end tell
end monthRange

set theYear to text returned of (display dialog "Please enter a year:" default answer (current date)'s year)

tell (choose from list {"January", "February", "March", "April", "May", "June", "July", "August", ¬
	"September", "October", "November", "December"} with prompt "Please choose a month:")
	if it is false then error number -128
	set theMonth to item 1
end tell

tell application "TextEdit"
	activate
	if not (exists document 1) then
		make new document at beginning with properties {name:"NewGigs"}
	end if
end tell

set {preDate, postDate} to monthRange(theMonth, theYear)

tell application "Palm Desktop"
	set myEvents to properties of events whose start time > preDate and start time < postDate and title contains "gig"
	my sortEvents(myEvents, 1, count myEvents)
	repeat with i in myEvents
		set gigName to title of i
		set beginTime to short date string of (start time of i)
		set endTime to short date string of (end time of i)
		set myHour to time string of start time of i
		set myEndHour to time string of end time of i
		set start_char to 1
		set end_char to length of myHour
		set all_chars to (characters of myHour)
		set end_char to -6
		try
			set myTime to text start_char thru end_char of myHour
			set myEndTime to text start_char thru end_char of myEndHour
		end try
		set a to (start time of i)
		set aa to a's short date string
		set new_Date to my FixDate(aa)
		
		set myStuff to {new_Date & " " & " - " & " " & myTime & ", " & new_Date & " " & " - " & " " & myEndTime & ", " & gigName} as string
		tell application "TextEdit"
			activate
			set last paragraph of document 1 to myStuff & return & return
		end tell
	end repeat
	tell application "TextEdit"
		activate
	end tell
end tell

This does what I need it to do…except now, I am using CMS Made Simple, a great, opensource web content management system. Its calendar module wants dates as yyyy-mm-dd; the applescript gives them to me as m/dd/yyyy.

Sooo, I need some assistance in making this conversion and having it fit into this applescript.

Any takers?

Many thanks,
Todd Reid

Model: G5 dual 2.0
Browser: Firefox 2.0.0.6
Operating System: Mac OS X (10.4)

Hi,

replace

to FixDate(b)
   set new_Date to {}
   repeat with c from 1 to 2
       if (b's word c) < 10 then
           set end of new_Date to ("0" & (b's word c) & "/")
       else
           set end of new_Date to ((b's word c) & "/")
       end if
   end repeat
   set end of new_Date to "20" & (b's word 3)
   return (new_Date as string)
end FixDate

with

to FixDate(b)
	tell b to return (its year as string) & "-" & my addZero(its month as integer) & "-" & my addZero(its day)
end FixDate

on addZero(v)
	return text -2 thru -1 of ("0" & v as string)
end addZero

and

set a to (start time of i)
set aa to a's short date string
set new_Date to my FixDate(aa)

with


set a to (start time of i)
set new_Date to my FixDate(a)

Stefan,
Many, many thanks…that worked a charm!!!

Best to you,
Todd

It’s actually still easier :wink:

Replace the new lines

set a to (start time of i)
set new_Date to my FixDate(a)

with

set new_Date to text 1 thru 10 of ((start time of i) as «class isot» as string)

and delete the handlers
to FixDate(b) and on addZero(v)

Stefan,
All seems good except for one thing: the hours are all am.

For example, a gig that goes from 7pm to 10pm is listed as 7am to 10am. Is this a 12-hour vs. 24-hour clock issue?

Help!

Todd

FYI, short date string and time string are dependent on your International settings in System Preferences.

I don’t see where it is that you’re having problem with the time…

Cool…so now, I just need to change this setting at the beginning of the script to a 24-hour clock and then change it back to the 12-hour at the end.

Any ideas on this one?
Todd

You should use a different method when doing calculations. (The various string properties of the date are best used when you’re just displaying dates to the user.)

Edit: FYI, this uses 24 hours:

hours of (current date)

Would something like this work for you?

set eventList to {{title:"big gig", start_time:current date, end_time:(current date) + 1 * hours}} -- Example
set gigList to {}

repeat with thisItem in eventList
	set thisName to title of thisItem
	set thisStart to formatDate(start_time of thisItem)
	set thisEnd to formatDate(end_time of thisItem)
	
	set end of gigList to thisStart & ", " & thisEnd & ", " & thisName
end repeat

set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to return & return
set gigList to "" & gigList
set AppleScript's text item delimiters to ASTID

tell application "TextEdit"
	activate
	
	if not (exists document 1) then
		make new document at beginning with properties {name:"NewGigs"}
	end if
	
	tell document 1 to set its text to its text & gigList
end tell

on formatDate(theDate)
	-- do shell script "/usr/bin/php -r 'echo date(\"m/d/Y - g A\", strtotime(\"" & theDate & "\"));'"
	do shell script "/usr/bin/php -r 'echo date(\"Y-m-d - h \", strtotime(\"" & theDate & "\"));'"
end formatDate

Bruce,
If you look at the script above, what I’m doing is grabbing events out of my Palm Desktop calendar, writing these to a CSV file, so that I can post on my website in the format that the calendar module of my CMS requires. It assumes a 24-hour clock, so really, all I need to do at this point is grab the beginning time and end time of each event in a 24-hour format.

Todd

I edited the formatDate() handler in the script above…

Does this work for you? (I can’t test it.)

set theYear to text returned of (display dialog "Please enter a year:" default answer (current date)'s year)

tell (choose from list {"January", "February", "March", "April", "May", "June", "July", "August", ¬
	"September", "October", "November", "December"} with prompt "Please choose a month:")
	if it is false then error number -128
	set theMonth to item 1
end tell

tell application "TextEdit"
	activate
	if not (exists document 1) then
		make new document at beginning with properties {name:"NewGigs"}
	end if
end tell

set {preDate, postDate} to monthRange(theMonth, theYear)

tell application "Palm Desktop"
	set myEvents to properties of events whose start time > preDate and start time < postDate and title contains "gig"
	my sortEvents(myEvents, 1, count myEvents)

	repeat with i in myEvents
		set gigName to title of i
		set beginTime to formatDate(start time of i) of me
		set endTime to formatDate(end time of i) of me
		
		tell application "TextEdit"
			activate
			set last paragraph of document 1 to beginTime ", " & endTime & ", " & gigName & return & return
		end tell
	end repeat

	tell application "TextEdit" to activate
end tell

on sortEvents(a, l, r)
	script o
		property p : a
	end script

	using terms from application "Palm Desktop"
		repeat with i from l + 1 to r
			set thisEvent to o's p's item i
			set thisTime to thisEvent's start time

			repeat with j from i to l by -1
				if (j > l) and (thisTime comes before o's p's item (j - 1)'s start time) then
					set o's p's item j to o's p's item (j - 1)
				else
					set o's p's item j to thisEvent
					exit repeat
				end if
			end repeat
		end repeat
	end using terms from
end sortEvents

on monthRange(m, y)
	tell {(date (m & " 1 " & y)) - 1}
		set end to beginning + 32 * days + 1
		set end's day to 1
		it
	end tell
end monthRange

on formatDate(theDate)
	do shell script "/usr/bin/php -r 'echo date(\"Y-m-d - H:i\", strtotime(\"" & theDate & "\"));'"
end formatDate

Bruce,
Unfortunately, it does not work. It give the following:

“PalmDesktop got an error: Can’t continue with formatDate.”

Any ideas?
Todd

:rolleyes: I shall refer myself to the documentation: Scope of Subroutine Calls in Tell Statements

I’ve edited the script above to include of me.

Yes, works just great…one more little thing (and I’ll send you a free CD!!! Email me your address!!!)

We have hours, but not minutes. I know the Unix for minutes is %M, but this doesn’t fit here.

Can you add that, or just tell me how to get minutes as well, and I’ll add it in?

Thanks,
Todd

I edited the above script to include the minutes.

For reference, the possible values are listed here: http://us.php.net/date

Bruce,
You are the dude! Can’t thank you enough!

Feel free to email me your mailing address, and I’ll send you a jazz CD that I did not long ago.

Best,
Todd

Bruce,
I have asked a lot of you already, but it seems I’m still getting 12-hour, not 24-hour times out of this script; the events that are supposed to be pm are still am.

Any ideas on this?
Todd

I missed that earlier. It’s fixed in the script above.

These should also work:

on formatDate(someDate)
	tell someDate to tell ((100000000 + ¬
		(its year) * 10000 + ¬
		(its month) as integer) * 100 + ¬
		(its day)) as string to ¬
		set theDate to text 3 thru 6 & "-" & text 9 thru 10 & "-" & text 11 thru 12
	
	tell time of (someDate) to tell (1000000 + ¬
		it div hours * 10000 + ¬
		it mod hours div minutes * 100 + ¬
		it mod minutes) as string to ¬
		set theTime to text 2 thru 3 & ":" & text 4 thru 5
	
	return theDate & " - " & theTime
end formatDate
on formatDate(someDate)
	tell (someDate as «class isot» as string) to return text 1 thru 10 & " - " & text 12 thru 16
end formatDate

Bruce,
YES, YES, YES!!!

Can’t thank you enough! I’m looking forward to looking through your script additions and learning from them.

Kind regards,
Todd