Retrieve dates of current week

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Retrieve dates of current week

Post by Nakia » Thu Mar 28, 2013 7:18 am

Hi,

Hit a bit of a stumble with a current project that I have not encountered before:
I need to be able to create a list of the all the dates in the current week..(Monday-Sunday)

Example
Query runs on Friday so get dates from Monday before though till Sunday..
I will also need to expand on this also to create a list for the next week..

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: Retrieve dates of current week

Post by shaosean » Thu Mar 28, 2013 8:29 am

Take a look at the convert command using the dateItems keyword

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

Re: Retrieve dates of current week

Post by Dixie » Thu Mar 28, 2013 11:56 am

Nakia...

The script beow will return the dates from Monday - Sunday...

Code: Select all

on mouseUp
   put theWeekSeconds(the long date) into red
   put item 1 of red into green
   put item 2 of red into blue
   
   convert green to short date
   convert blue to short date
   set itemdel to "/"
   put item 2 of green & "/" & item 1 of green & "/" & item 3 of green & comma & \
         item 2 of blue & "/" & item 1 of blue & "/" & item 3 of blue
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

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

Re: Retrieve dates of current week

Post by bn » Thu Mar 28, 2013 1:47 pm

Hi,

here is another take on this, does not use seconds just dateItems, assumes Sunday is the first day of week.
Using just dateItems avoids any problems with daylight saving.

Code: Select all

on mouseUp
   put the date into tDate
   convert tDate to dateItems
   put last item of tDate into tDayOfWeek
   subtract tDayOfWeek - 1 from item 3 of tDate -- lets start sundays
      repeat 7 -- days
         put tDate into tDateCalc
         convert tDateCalc to short date
         --convert tDateCalc to long date
         put tDateCalc & comma after tCollect
         add 1 to item 3 of tDate
      end repeat
      delete last char of tCollect -- a comma
      put tCollect into field 1
end mouseUp
also check system date for a localized date format.

Kind regards
Bernd

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: Retrieve dates of current week

Post by Nakia » Thu Mar 28, 2013 10:09 pm

Thanks everyone,

BN: to expand on your solution for retrieving the dates for the next week
Is it true that I change the line that is calculating the start day?

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

Re: Retrieve dates of current week

Post by bn » Thu Mar 28, 2013 10:24 pm

Hi Nakia,

it depends on what you want. If you want the current week and the following week in one go then you would just change the repeat loop from 7 to 14. It will give you 2 weeks of dates starting at last Sunday of current week.

If you want just the dates of the week following the current week then you would change

Code: Select all

subtract tDayOfWeek - 1 from item 3 of tDate -- lets start sundays
to start on coming sunday. Hint you would have to add instead of subtract the right amount. Set a breakpoint and look in the debugger what happens in the dateItems.

Kind regards
Bernd

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: Retrieve dates of current week

Post by Nakia » Fri Mar 29, 2013 10:28 pm

Thanks again for everyone help!!

I ended up just extending the loop for 14 days then adding an if condition that determined which variable to store the dates in :D

Code: Select all

put the date into tDate
   convert tDate to dateItems
   put last item of tDate into tDayOfWeek
   subtract tDayOfWeek -2  from item 3 of tDate ## This line calculates when to start
   repeat with x = 1 to 14 ## This is the amount of days to collect
      put tDate into tDateCalc
      convert tDateCalc to short date
      ## Convert tDateCalc to long date
      if x > 7 then
         put tDateCalc & return after lNextWeekDates
      else
         put tDateCalc & return after lCurrentWeekDates
      end if
      add 1 to item 3 of tDate
   end repeat
   delete last char of lCurrentWeekDates
   delete last char of lNextWeekDates

Post Reply