Page 1 of 1

Date calculations...

Posted: Tue Oct 04, 2011 11:21 am
by hoburne
Hi,

I am new to livecode, (also a little hungover which isnt helping) and I am trying to write a function to return the date in seconds of the start of this week (closest monday just gone) and end of the week (next sunday) based on todays date.

I dont know where to start - perhaps coffee and headache tablets?

Can anyone point me in the right direction with this, really appreciate some help.

Thanks.

Re: Date calculations...

Posted: Tue Oct 04, 2011 12:36 pm
by SparkOut
Hi hoburn

You can look up the "convert" instruction in the dictionary which will give you lots of pointers about how to manipulate dates.

For your task, a function like

Code: Select all

function getLastMonday
   put the date into tDate
   convert tDate to dateItems
   put item 7 of tDate into tDayNumber
   -- Sunday = day 1, through Saturday = day 7
   -- so if today is Tuesday then tDayNumber = 3
   -- the previous Monday is therefore:
   -- (today's day number minus 2) days before today
   -- ie for Tuesday it is (3 minus 2) days ago (that would be 1 day beforehand)
   -- except if today is Sunday then it is 7 minus (tDayNumber minus 2) = 6 days beforehand
   if tDayNumber = 1 then
      put 6 into tDaysToSubtract
   else
      put tDayNumber - 2 into tDaysToSubtract
   end if
   -- now subtract the right number of days from the item in the list of dateItems that corresponds to the day
   -- note Livecode will take care of the wrapping of one month to another if necessary, don't worry about negatives here
   put item 3 of tDate - tDaysToSubtract into item 3 of tDate
   convert tDate to seconds
   return tDate
end getLastMonday
should work, and give you enough pointers to write a function to achieve the same when looking for next Sunday. If you need any more clarification, just say.

Re: Date calculations...

Posted: Tue Oct 04, 2011 12:58 pm
by Dixie
Hi...

Code: Select all

on mouseUp
   put theWeekSeconds(the long date)
end mouseUp

function theWeekSeconds today
      put lineOffset( item 1 of today, the weekDayNames) into dayCheck
      convert today to seconds
      
      if dayCheck = 1 then
            put today into theWeekStart
            put today + (86400 * (7 -dayCheck)) into theWeekEnd
      else
            put today - (86400 * (dayCheck - 1)) into theWeekStart
            put today + (86400 * (7 -dayCheck)) into theWeekEnd
      end if
   
      return theWeekStart & "," & theWeekEnd
end theWeekSeconds
Hope it helps

Dixie

Re: Date calculations...

Posted: Tue Oct 04, 2011 1:14 pm
by hoburne
Thanks SparkOut and Dixie. Very helpful both work perfectly. Really helped my understanding of date manipulation :)

Re: Date calculations...

Posted: Tue Oct 04, 2011 2:42 pm
by kevin11
Isn't the Sunday always 518400 seconds after last Monday, so once you've got Monday you're sorted ?

for the hangover, try :

put empty into sugar
put empty into milk
put "coffee" & milk & sugar into throat

Re: Date calculations...

Posted: Wed Oct 05, 2011 12:50 pm
by Dixie
kevin11 wrote:Isn't the Sunday always 518400 seconds after last Monday, so once you've got Monday you're sorted ?
It is... :D , but around here in the 'sleepy' villages of Somerset, Sunday is often cancelled which throws as spanner into the works as far as date calculations are concerned...

be well

Dixie