| |||
| [Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] |
+--> John Delacour wrote 03-08-20:_______________________________________________At 2:42 pm -0500 20/8/03, Rich Carroll wrote:+-Is there a simple way to coerce "August 20, 2003" to 08/20/03?
I can figure out a complicated series of steps to do it "manually," I'm just
hoping someone knows a better way.
You can do it in the shell or with perl:
set d1 to "1/1/70"
set d2 to "August 20, 2003"
set d to (get date d2) - (get date d1) -- or (current date) - (get date d1)
do shell script "perl -e '@t = localtime " & d & ";
printf qq~%02d/%02d/%02d~, $t[4]+1,$t[3],$t[5] ;'"
Just beware that this is locale dependent.
Not only d2, but more importantly: d1 is US-only!
Might be OK for Rich, but don't distribute the code...
If you hand Perl the difference between the date and current date the thing is avoided.
For those interested, I give you the whole handler I did a year ago or so, and which is locale independent (i.e should be internationally OK). Adapt to US date style if you want that instead of the ISO standard.
on isodate(arg)
-- Will do conversion between an AppleScript date and a datestring given in the form "yyyy-mm-dd".
-- Give it either form, and it will compute the other form.
-- Dates must be within years 1902 and 2038.
-- Conversion to AppleScript date will set the time to around noon, +- 1 hour depending on
-- if a daylight saving switch has occured since the argument date (a problem caused by
-- AppleScript not taking change of DLST into account when calculating elapsed time,
-- whereas Perl does take that into account!).
-- Harald E Brandt, bragit.com
if class of arg is string then
if length of arg is not 10 then error "wrong datestring format"
do shell script "perl -e '$_ = q(" & arg & ");
use Time::Local;
/(\\d{4})-(\\d{2})-(\\d{2})/;
print time - timelocal(0,0,12,$3,$2-1,$1); #noon to avoid possible DLST problems
'"
(current date) - (result as number)
else if class of arg is date then
set time of arg to 43200 --noon to avoid possible DLST problems
(current date) - arg
do shell script "perl -e '$_ = " & result & ";
@_ = localtime (time - $_);
printf q(%04s-%02d-%02d), (1900+$_[5], 1+$_[4], $_[3]);
'"
else
error "wrong class given to isodate handler"
end if
end isodate
| Home | Archives | FAQ | Terms/Conditions | Contact | RSS | Lists | About |
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE
Contact Apple | Terms of Use | Privacy Policy
Copyright © 2007 Apple Inc. All rights reserved.