Hi!!
I have this form to get date in OS X:
set month to (do shell script “date +%m”) as string
set day to (do shell script “date +%d”) as string
but it does not works in OS 9.
Could somebody elp me?
Thanks,
vias
Hi!!
I have this form to get date in OS X:
set month to (do shell script “date +%m”) as string
set day to (do shell script “date +%d”) as string
but it does not works in OS 9.
Could somebody elp me?
Thanks,
vias
do shell script is OS X only.
To get the date, use:
get current date
To get the day and month:
tell (current date)
set currentDay to day
set currentMonth to month of it -- must use 'month of it', not 'month', when using a tell statement for some reason
end tell
display dialog "Day: " & currentDay & return & "Month: " & currentMonth
Hope this helps.
Thanks, but I need the month as integer, but OS 9 can’t set it to integer.
Thanks!!!
Hi, jj.
The code shown in the FAQ is Emmanuel’s brilliant original. A slightly shorter and (in my view) easier to remember version is:
set theDate to (current date) -- or any other date
copy theDate to b
set b's month to January
set monthNum to (b - 2500000 - theDate) div -2500000
2500000 is a conveniently round number in the range that happens to work with this method (which is any number of seconds between 28 days and about 29.5 days). The “double negative” in the calculation is to get round an AppleScript bug that caused the method to error with dates prior to 1904. (This bug has only just been fixed in Tiger ” unless it was fixed in Panther and no-one noticed!)
Above reduced to one line as follows:
1 + (((day of (current date)) * days) + (((year of (current date)) - 1904) * 365.25 * days) ¬
- ((current date) as integer) as integer) div -2500000
Sorry last post should read:
set monthNum to 1 + (((day of (current date)) * days) + (((year of (current date)) - 1904) * 365.25 * days) ¬
- ((current date) as integer) as integer) div -2500000
Actually, it is possible to get the number of the current month (only) in just one line, like this:
set currMonthNum to ((date (get "1/1")) - 2500000 - (get date (get "1"))) div -2500000
It’s the same maths as French Vanilla. Somewhat slower, though.
I’m pretty sure that the problem here is caused by reliance on a third-party OSAX. If I assume that this hypothetical OSAX provides date-to-integer coercions, and that the results of the coercions are numbers of seconds since 1st January 1904 00:00:00, then the method works very well.
set monthNum to 1 + (((day of (current date)) * days) + (((year of (current date)) - 1904) * 365.25 * days) ¬
- ((current date) - (date "Friday 1 January 1904 00:00:00")) as integer) div -2500000
The number of calls to ‘current date’ can be reduced by means of a ‘tell’, and ‘365.25 * days’ can be precalculated to save doing it and the other integer coercion at run time:
tell (current date) to set monthNum to 1 + (((its day) * days) + (((its year) - 1904) * 31557600) ¬
- (it - (date "Friday 1 January 1904 00:00:00"))) div -2500000
This obviously allows other dates to be slotted in in place of the current one. The method doesn’t work over the whole of the AppleScript date range, but it’s OK within most of our lifetimes and is nearly as fast as French Vanilla itself.
I tried it with a couple of my favourite OSAXen but, with or without them, it doesn’t work here, either.
If it’s all the same to you, Nigel, I don’t mind waiting around long enough to witness its failure…
Yeah. Sure. We must organise a failure watching party. I’ll … er … leave you to sort out the date and time.
We must organise a failure watching party. I’ll … er … leave you to sort out the date and time.
:lol: On reflection, since that gives us less than a century, perhaps we can wait for this version to fall over, instead…
tell (current date) to set monthNum to 1 + ((day * days) + ((year - 1904) * 31556900) - (it - (date "Friday, January 1, 1904 00:00:00"))) div -2500000