Re: Date formatting
Re: Date formatting
- Subject: Re: Date formatting
- From: Paul Berkowitz <email@hidden>
- Date: Tue, 04 Mar 2003 08:22:55 -0800
On 3/4/03 2:25 AM, "John Stewart" <email@hidden> wrote:
>
do shell script "date + %m/%d/%y"
>
--> "03/04/03"
>
>
>
Use "man date" (no quotes and no pun intended) in the Terminal for the
>
Unix man page on its date command. The drawback is that it only works
>
with the current date. Others on the list (that are better at Unix) can
>
probably supply a method for formatting other than the current date.
Follow the links at 'man date' to 'man strftime' and you'll see all the
various date forms that can be used. 'man date' itself explains that you can
get any date by using the -r option with the number of seconds since Jan 1,
1970 GMT. So the trick is to get that very large number without the "E+"
format that AppleScript uses for large numbers:
set theDate to date "Sunday, March 17, 2002 12:00:00 AM" -- for example
set uniDate to theDate - (time to GMT)
set unixSecs to uniDate - (date "Thursday, January 1, 1970 12:00:00 AM")
set unixSecs to my NumberToString(unixSecs)
set theDate to do shell script "date -r " & unixSecs & " \"+%m/%d/%y\""
--there are lots and lots of other date formats you can use
--> "03/17/02"
on NumberToString(bigNumber)
set bigNumber to bigNumber as string
if bigNumber does not contain "E+" then return bigNumber
set {tids, AppleScript's text item delimiters} to {AppleScript's text
item delimiters, {"E+"}}
try
set {basicReal, powersOfTen} to {bigNumber's text item 1,
(bigNumber's text item 2) as integer}
on error -- e.g. Python strings have lower-case "e", tids case-sensitive
set AppleScript's text item delimiters to {"e+"}
set {basicReal, powersOfTen} to {bigNumber's text item 1,
(bigNumber's text item 2) as integer}
end try
set AppleScript's text item delimiters to {"."}
try
set {integerPart, decimalPart} to basicReal's {text item 1, text
item 2}
on error
set AppleScript's text item delimiters to {","}
set {integerPart, decimalPart} to basicReal's {text item 1, text
item 2}
end try
set AppleScript's text item delimiters to tids
set n to count decimalPart
if powersOfTen n then
repeat (powersOfTen - n) times
set decimalPart to decimalPart & "0"
end repeat
set bigNumber to integerPart & decimalPart
else
set bigNumber to integerPart & decimalPart
set bigNumber to text 1 thru (powersOfTen + 1) of bigNumber & "." &
text (powersOfTen + 2) thru -1 of bigNumber
end if
return bigNumber
end NumberToString
--
Paul Berkowitz
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.