Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Convert date to a week number

Currently I use user input to work this out.


like this


display dialog thePrefix & "Please enter the week number:" default answer theWeekNumber

set theWeekNumber to text returned of result

display dialog thePrefix & "Please enter the Month:" default answer theMonth

set theMonth to text returned of result



For the month line I need it to convert say the month we are in to "08 August" I like to use the "08" as it helps keep the order in list view consistant.


Now for the week number it is more complicated as I need it to work with our work calender.


I use a similare method in excel the calulation takes this form.


=INT(((NOW())-DATE(YEAR((NOW())-31),-3,-14)-WEEKDAY((NOW()),1))/7)+2



Monday August 20th will be week1.


How can i get applescript to get the date and convert to week number and also the month.


Many Thanks


Matt

iMac, Mac OS X (10.6.8)

Posted on Aug 1, 2012 3:22 AM

Reply
19 replies

Aug 1, 2012 5:49 AM in response to MattJayC

I did find this,


on weekNumberfrominputDateagainstweekStart


-- Get a known date in the past with the week-start weekday.


-- NB. This was compiled in Snow Leopard, where AppleScript dates are Julian rather than Gregorian before some date in the 1500s. With Leopard or earlier, use date "Monday 6 January 1000".

set baseDate to (date "Monday, 1 January 1000 00:00:00") + (offset of (text 1 thru 2 of (weekStart as text)) in "MoTuWeThFrSaSu") div 2 * days



-- Get 4th January in the year of the input date.

copy inputDate to Jan4

set Jan4's day to 4

set Jan4's month to January


-- If the input date's in the last three days of December, initially try with 4th January the following year.

if (inputDate's month is December) and (inputDate's day > 28) then set Jan4's year to (Jan4's year) + 1



-- Derive the beginning of the week of the 4th January date.

set yearStart to Jan4 - (Jan4 - baseDate) mod weeks


-- If the input date comes before this, it's counted as being in the previous year.

if (inputDate comes before yearStart) then

set Jan4's year to (Jan4's year) - 1

set yearStart to Jan4 - (Jan4 - baseDate) mod weeks

end if



-- Derive and return the relevant week number for the input date.

return (inputDate - yearStart + weeks) div weeks

end weekNumber


weekNumberfrom (current date) againstMonday-- or "Monday"


Just trying to figure out how to use it?

Aug 1, 2012 6:05 AM in response to MattJayC

Most likely the script does not know your timezone and is giving you the GMT equivalent (I know GMT is no longer the official 0 offset timezone, I just forget what they call it these days 🙂 ).


You could try:


set myDate to do shell script "TZ=EST5EDT date '+%V %B'"


where you substitute your timezone information. The EST5EDT is for U.S Eastern Standard Time, 5 hours behind GMT, Eastern Daylight Saving Time.

Aug 1, 2012 6:27 AM in response to BobHarris

Both methods are giving me the right thing, I think I have missed a detail in my explanation.


Week Number I need my result to be just a week number.


so


set myDate to do shell script "TZ=EST5EDT date '+%V'"


works as does


set myDate to do shell script "date '+%V'"


I need to take the result and add 19 and that will give me the result I need?


Now the month thing is slightly differeent I need the number of the month followed by the name of the month.


August is the 8th month so I need the result to be


08 August.


Thanks so far!!

Aug 1, 2012 1:33 PM in response to MattJayC

Hi,


MattJayC wrote:


NICE WORK I couldn't figure it out


set myDate to do shell script "date -v+133d +%V"


Adding 133 days in this command is not accurate (depending on the year).

Here are my tests :

Mon 24 Aug 2015 --> 01 , must be 02

Mon 17 Aug 2015 --> 53 , must be 01

Mon 18 Aug 2014 --> 01 , OK

Mon 19 Aug 2013 --> 01 , OK

Mon 20 Aug 2012 --> 01 , OK

Mon 22 Aug 2011 --> 01 , must be 02

Mon 15 Aug 2011 --> 52 , must be 01

Mon 23 Aug 2010 --> 01 , must be 02

Mon 16 Aug 2010 --> 52 , must be 01

Mon 24 Aug 2009 --> 01 , must be 02

Mon 17 Aug 2009 --> 52 , must be 01




if you want accuracy, here is the script :

---------------------------------------------------------------

setthisDatetocurrent date


---- your first week of the year is the third Monday of August

setweekNumberto ((thisDate - (myaugustThirdWeek(thisDate))) div 604800) + 1


onaugustThirdWeek(d) -- get the third monday of August

copy d to tDate

tell tDate

set {its day, its month, its time} to {15, 8, 0} -- start at August 15

repeat 2 times

repeat while (its weekday) is not Monday

set its day to (its day) + 1 -- add one day (range 16 ... 21)

end repeat

ifd < itthen -- this date is older that the third Monday of August

set {its day, its year} to {15, (its year) - 1} -- subtract one year

else

return it

end if

end repeat

endtell

endaugustThirdWeek

Aug 2, 2012 1:26 AM in response to Jacques Rioux

I did wonder if a few irregularites like that might happen.


I just did a run through and the result was 51 (I was expecting 50)


But it is right the 3rd Monday of August (sunday to be precise but never in on Sunday so difference) Can't quite work out where I need to make the alteration.


Also is "augustThirdWeek" my value that i can then use within a script? i.e. augustThirdWeek = 51


Thanks


Matt

Convert date to a week number

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.