Date calculations...
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Date calculations...
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.
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...
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 likeshould 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.
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
Re: Date calculations...
Hi...
Hope it helps
Dixie
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
Dixie
Last edited by Dixie on Wed Oct 05, 2011 4:25 pm, edited 1 time in total.
Re: Date calculations...
Thanks SparkOut and Dixie. Very helpful both work perfectly. Really helped my understanding of date manipulation 

Re: Date calculations...
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
for the hangover, try :
put empty into sugar
put empty into milk
put "coffee" & milk & sugar into throat
Re: Date calculations...
It is...kevin11 wrote:Isn't the Sunday always 518400 seconds after last Monday, so once you've got Monday you're sorted ?

be well
Dixie