Hi,
I’m writing a little script that puts stuff from a txt file into iCal.
How can i change 01/09/2006 into 09/01/2006 easily ?
Thanks in advance,
Karl
Hi,
I’m writing a little script that puts stuff from a txt file into iCal.
How can i change 01/09/2006 into 09/01/2006 easily ?
Thanks in advance,
Karl
Karl, your example does not best explain the order you want, month and day could be either way around. Here is how I would do this.
-- Date as dd/mm/yyyy
set TodaysDateA to do shell script "date \"+%d/%m/%Y\""
-- Date as mm/dd/yyyy
set TodaysDateB to do shell script "date \"+%m/%d/%Y\""
set theDate to my changeFormat("01/09/2006")
on changeFormat(theDate)
set {olddelims, text item delimiters} to {text item delimiters, "/"}
set {d, m, y} to (text items of theDate)
set retVal to {m, d, y} as string
set text item delimiters to olddelims
return retVal
end changeFormat
or
set theDate to my changeFormat("01/09/2006")
on changeFormat(theDate)
return (do shell script "echo " & theDate & " | awk -F '/' '{printf\"%s/%s/%s\",$2, $1, $3}'")
end changeFormat
Dominik; That is a really slick and compact way to do that It works equally nicely with text for interchanging the first two blocks:
on changeFormat(theDate, delim)
set {olddelims, text item delimiters} to {text item delimiters, delim}
set {d, m, y} to (text items of theDate)
set retVal to {m, d, y} as string
set text item delimiters to olddelims
return retVal
end changeFormat
set theText to my changeFormat(" eats, shoots, and leaves", ",") --> shoots, eats, and leaves
You do not even need to use text item delimiters for this exercise in plain AppleScript. Here are two methods that I use from time to time, depending my moods:
set theDate to short date string of (current date)
set newDate to (theDate's word 2) & "-" & (theDate's word 1) & "-" & (theDate's word 3) --> "26-9-06"
I prefer dashes to slashes, but you can put whatever character you want in between the digits.
If you like words instead of digits, this works well:
set theDate to (current date)
set newDate to ((theDate's day) & " " & (theDate's month) & " " & (theDate's year)) as string --> "26 September 2006"
True, Craig, but this line: “set {d, m, y} to (text items of theDate)” was what intriged me. I keep forgetting to use that construct in lots of contexts where it’s very useful, e.g.,
set theName to "Craig Smith"
set newName to changeFormat(theName, " ", ", ") --> Smith, Craig
on changeFormat(FirstLast, delim1, delim2)
set {olddelims, text item delimiters} to {text item delimiters, delim1}
set {tFirst, tLast} to (text items of FirstLast)
set text item delimiters to delim2
set retVal to {tLast, tFirst} as string
set text item delimiters to olddelims
return retVal
end changeFormat
Ah, I think I see what you mean. How about this then for simple and brief:
set {a, b, c} to ((short date string of (current date))'s words)
set newDate to b & "-" & a & "-" & c --> "26-9-06"
Or if we’re pushing for short :lol: :
tell short date string of (current date) to word 2 & "." & word 1 & "." & word 3
Similarly:
tell "Adam Bell" to word 2 & ", " & word 1 ---> Bell, Adam
Hey, that is slick! I was trying to figure out how to do this in one line, but forgot about “telling the string.” Thanks, Adam.
Not as slick, but this could be useful for those who want to convert dates back and forth:
(note: if you just want the short date string use Adam’s script above)
on americanDateStringToDate(americanDateString)
tell americanDateString to set {theDate, day of theDate} to {my date (word 1 & space & word 1 & space & text (word 3) thru -1), word 2}
return theDate
end americanDateStringToDate
on britishDateStringToDate(britishDateString)
tell britishDateString to set {theDate, day of theDate} to {my date (word 2 & space & word 2 & space & text (word 3) thru -1), word 1}
return theDate
end britishDateStringToDate
{american:americanDateStringToDate("10/1/2006 6:00"), british:britishDateStringToDate("10/1/2006 23:59:04")}
--> {american:date "Sunday, 1 October 2006 6:00:00 AM", british:date "Tuesday, 10 January 2006 11:59:04 PM"}