Date calculations...

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
hoburne
Posts: 40
Joined: Tue Oct 04, 2011 11:13 am

Date calculations...

Post by hoburne » Tue Oct 04, 2011 11:21 am

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.

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: Date calculations...

Post by SparkOut » Tue Oct 04, 2011 12:36 pm

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.

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Date calculations...

Post by Dixie » Tue Oct 04, 2011 12:58 pm

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
Last edited by Dixie on Wed Oct 05, 2011 4:25 pm, edited 1 time in total.

hoburne
Posts: 40
Joined: Tue Oct 04, 2011 11:13 am

Re: Date calculations...

Post by hoburne » Tue Oct 04, 2011 1:14 pm

Thanks SparkOut and Dixie. Very helpful both work perfectly. Really helped my understanding of date manipulation :)

kevin11
Posts: 101
Joined: Thu Aug 11, 2011 5:02 pm

Re: Date calculations...

Post by kevin11 » Tue Oct 04, 2011 2:42 pm

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

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Date calculations...

Post by Dixie » Wed Oct 05, 2011 12:50 pm

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

Post Reply