Date Calculation

Alex Tweedly alex at tweedly.net
Thu Nov 29 16:10:04 EST 2012


On 29/11/2012 20:51, J. Landman Gay wrote:
> On 11/29/12 2:23 PM, Andrew Kluthe wrote:
>> That's what I'm looking for! Great.
>>
>> However, I tried what I think I am supposed to do in the script below
>> and my dates are about three days off.
>>
>> This function is supposed to give me the date for monday of the week
>> where the date paramater is.
>>
>> If the paramater is empty it uses todays date.
>>
>> function DetermineWeek pDate
>>     if pDate is empty then
>>        put the date into sDate
>>     else
>>        put pDate into sDate
>>     end if
>>     convert sDate to dateItems
>>     answer sDate
>>     set the itemDelimiter to ","
>>     put item -1 of sDate into sDay
>>     switch sDay
>>        case "2" --Monday
>>           put 0 into sDiff
>>        case "3" --Tuesday
>>           put 1 into sDiff
>>        case "4" --Wednesday
>>           put 2 into sDiff
>>        case "5" --Thursday
>>           put 3 into sDiff
>>        case "6" --Friday
>>           put 4 into sDiff
>>        case "7" --Saturday
>>           put 5 into sDiff
>>        case "1" --Sunday
>>           put 6 into sDiff
>>     end switch
>>     put item 3 of sDate into sDayOfMonth
>>     put sDayOfMonth - sDiff into sNewDay
>>     put sNewDay into item 3 of sDate
>>     answer sDate
>>     convert sDate to date
>>     return sDate
>> end DetermineWeek
>>
>> For instance leaving the paramater blank should provide me today
>> (11/29/12) with a date of 11/26/12 but instead I am returned 11/23/12.
>
> You forgot the "break" statements in the switch. Everything will fall 
> through to a difference of 6.
>
Or just replace  all that switch by
>     put item -1 of sDate into sDay
>     put sDay-2 into sDiff
>     if sDiff < 0 then put 6 into sDiff

or do it all in one step
>    put item 3 of sDate - (item -1 of sDate) + 2 into sDayOfMonth
>    if item -1 of sDate = 1 then subtract 7 from sDayOfMonth
>    put sDayOfMonth into item 3 of sDate

-- Alex.




More information about the use-livecode mailing list