Re: Current date notation
Re: Current date notation
- Subject: Re: Current date notation
- From: Sun Real <email@hidden>
- Date: Fri, 11 May 2001 13:49:52 +1000
Pier Kuipers' message of 11/5/01 6:19 AM contained:
>
The problem arises when you get to the month part of the current date:
>
>
set theMonth to month of (current date)
>
--> May
>
>
So you'll need a little script step to work out what the month's
>
number is. There has been a few discussions about how to do this on
>
this list, but I use a repeat statement to check through a list of
>
the month names.
>
Another problem is the leading zero infront of days or months that
>
are less than 10, so another step is required here. In all, I would
>
script something like the following:
>
>
property theMonthList : {January, February, March, April, May, June,
>
July, August, September, October, November, December}
>
>
set theMonth to month of (current date)
>
set theDay to day of (current date)
>
set theYear to year of (current date)
As Paul has already pointed out, it's much quicker to just call current
date once & get all this out of the date object.
>
set x to 1
>
repeat with i from 1 to 12
>
if (item x of theMonthList) is (theMonth) then
>
set y to x
>
exit repeat
>
else
>
set x to x + 1
>
end if
>
end repeat
This is a perfectly good method, though your use of 'x' as a loop counter
variable is not required. 'i' would work just as well.
Emmanuel's "French Vanilla" method (and variants) are much faster though.
Pass a date object:
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- -- -- -- -- -- -- -- -- -- -- -- --
-- -- monthNum -- -- -- -- from Emmanuel Levy -- returns
an integer
-- corresponding to the current month; called by the date handlers
on monthNum from dateObj
copy dateObj to dateTemp
set month of dateTemp to January
return (1 + (dateObj - dateTemp + 1314864) div 2629728)
end monthNum
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- -- -- -- -- -- -- -- -- -- --
>
if x < 10 then
>
set x to (0 & x) as text
>
end if
>
if theDay < 10 then
>
set theDay to (0 & theDay) as text
>
end if
This is fairly subtle, but, as Joe Barwell pointed out to me, it's
actually slightly faster to just add the leading zero than to test for
whether one's required or not:
set theDay to text -2 through -1 of ("0" & theDate's day)
>
>I'm looking for the syntax on current date notation. I want to have
>
>the output of "Current Date" as this:
>
>
>
>DD-MM-YYYY
I do it like this:
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- -- -- -- -- -- -- -- -- -- -- -- --
-- -- shortNumericDate -- -- -- -- returns dd-mm-yyyy
-- -- Richard Morton 1998-2001
on shortNumericDate from dateObj
return text -2 thru -1 of ("0" & dateObj's day) & "-" & ,
text -2 thru -1 of ("0" & (monthNum from dateObj)) & ,
"-" & ((year of dateObj) as text) -- this is all one line
end shortNumericDate
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- -- -- -- -- -- -- -- -- -- --
Note that it calls 'monthNum', so that must be in the same script as
this. It's fairly terse - you may want to pull it apart to see what's
going on - but it's pretty straightforward.
Cheers,
Richard Morton
-- Great Lies of the Music Business: "I practise 6 hours every day"