Page 1 of 1
Retrieve dates of current week
Posted: Thu Mar 28, 2013 7:18 am
by Nakia
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..
Re: Retrieve dates of current week
Posted: Thu Mar 28, 2013 8:29 am
by shaosean
Take a look at the
convert command using the
dateItems keyword
Re: Retrieve dates of current week
Posted: Thu Mar 28, 2013 11:56 am
by Dixie
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
Re: Retrieve dates of current week
Posted: Thu Mar 28, 2013 1:47 pm
by bn
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
Re: Retrieve dates of current week
Posted: Thu Mar 28, 2013 10:09 pm
by Nakia
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?
Re: Retrieve dates of current week
Posted: Thu Mar 28, 2013 10:24 pm
by bn
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
Re: Retrieve dates of current week
Posted: Fri Mar 29, 2013 10:28 pm
by Nakia
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
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