I’m running OS 9.2 on a G4. I need to identify the number of weeks from a starting date. We are currently on week 5. Our first week started on Saturday, Dec. 28, 2002. I can’t seem to get AS to track the weeks the way I can in FMPro.
Could you point me in the right direction or give me some insight?
Thanks,
tmw
I’m working on Mac OS 8.6 at home but this should still work for you.
There is probably a better way of doing this - I couldn’t figure out how to add days, or weeks to a Mac date in one line so I pieced together some math. It seems to work.
(*To get a valid date format for the Mac I got the current date
and copied it using your start date by adding a time to it*)
set StartWeek to date "Saturday, December 28, 2002 12:00:00 AM"
set SecondsInAWeek to 604800 --there are this many seconds in a week, unless you sleep a lot
set WeeksNum to 5 --set this to however many weeks you need
set SecondsToAdd to SecondsInAWeek * WeeksNum --multiply the number of weeks by the number of seconds in a week
set GetWeek to (date StartWeek) + SecondsToAdd as string --add the number of seconds to your start date (this gets the date as a string)
set GetWeekAsDate to (date (date StartWeek)) + SecondsToAdd --this gets the date as a date
--or to get the number of weeks that have passed since your start date
--set CurrentDate to current date--you could use this to get the current date if that is what you want to compare to
set CurrentWeek to date "Thursday, January 30, 2003 12:00:00 AM" --otherwise set a date for this demo
set theDiff to CurrentWeek - StartWeek as integer
set NumWeeks to theDiff / 604800 --subtract todays date from your start date and divide by the number of seconds in a week
(*This results in 4.714285714286 because it hasn't been quite 5 full weeks, but would if your
current date was Saturday, February 1, 2003 12:00:00 AM So, if you are going to figure
the number of weeks that have passed on a day that isn't, say in this case exactly 5
weeks out, you have the option of rounding the number up or down*)
set NumWeeksRounded to round NumWeeks--this will round to nearest
Other rounding options:
I’m thinking there may be a way to just add to a Mac date in weeks or days. All I could figure out was to add seconds. There are a couple of scripting additions that also allow you to do this in a few lines instead of crunching math like this.
You can also operate directly using certain AS date constants:
set first_week to date "Saturday, December 28, 2002 12:00:00 AM"
((current date) - first_week) / weeks
--> 4.9xxxxxx and upping! You're near to:
first_week + 5 * weeks
set startDate to date "Saturday, 28 December 2002 00:00:00"
set weekNumber to ((current date) - startDate) div weeks + 1
You could perhaps get AppleScript to calculate the start date for you too, but I’m not sure what your criteria are for this. If you want a week beginning on a Saturday which includes 1st January, then:
set startDate to current date
tell startDate to set {its month, its day, its time} to {January, 1, 0}
repeat until startDate's weekday is Saturday
set startDate to startDate - days
end repeat