Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Coerce time



First of all if you want to change a long date to a short date and you want to follow the localization for the user, do this:
---------
set myDate to date "August 20, 2003"
set myShortDate to short date string of myDate
---------

If you specifically want it in the format mm/dd/yy then you need to parse it out yourself:
---------
set myDate to date "August 20, 2003"
set myShortDate to ""

set theMonth to the (month of myDate) as integer
set theDay to the (day of myDate) as integer
set theYear to (text 3 thru 4 of ((year of myDate) as text))

if theMonth is less than 9 then
set myShortDate to myShortDate & "0"
end if
set myShortDate to myShortDate & theMonth & "/"

if theDay is less than 9 then
set myShortDate to myShortDate & "0"
end if
set myShortDate to myShortDate & theDay & "/"

set myShortDate to myShortDate & theYear
---------

- Ken

On Nov 30, 2003, at 6:55 PM, Harald E Brandt wrote:

+--> 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
_______________________________________________
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.



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.