how to get the next Friday

I would have thought that this would be a fairly straightforward command. Maybe its that I’ve been staring at the computer all day…

Anyway, all I want is to get the date of the next Friday after the current date. It’s not a matter of adding 7, because this could be run on any day of the week. Do I need to write an if/then for every day of the week where if its Tuesday, then add 3days to the date, if its Monday, then add 4days, etc…

Is there a shorter way?

And yes, I realize I could have written those 7 if statements by now, but this is more for my own curiosity…

Mike

This is what I’d use:

set theDate to (current date)
set nextFriday to theDate + (6 - (weekday of theDate) as integer) * 86400

try this:

tell (current date)
	set diff to 6 - (its weekday as integer)
	if diff < 1 then set diff to diff + 7
	set NextFriday to it - (its time) + diff * days
end tell

Edit: or as subroutine

set nextSunday to nextWeekday(Sunday)

on nextWeekday(w)
	tell (current date)
		set diff to (w as integer) - (its weekday as integer)
		if diff < 1 then set diff to diff + 7
		return it - (its time) + diff * days
	end tell
end nextWeekday

Here is yet another method:

set theDate to (current date)
repeat until (theDate's weekday) = Friday
	set theDate to theDate + 1 * days
end repeat

tell (current date) to set nextFriday to it - (it - (date "Friday 3 January 1000 00:00:00")) mod weeks + weeks

Applause

:lol:

Edit: PS: but it works only on english based systems, you have to adjust the date string for non english sytems

Awesome!:D:cool:

For a full display of Nigel on Dates see DateTips 1.1 in ScriptBuilders

Thank you. Thank you. :wink:

As text, yes. But once the script’s compiled, it’ll work on any system. If I were to compile it on my machine and e-mail to you, you’d see the date in your own language when you opened it in Script Editor. Similarly with a script from you to me.

Other observations that might be made with the various methods:

  1. Craig’s method and mine work on any system. (The weekday → integer coercion was only introduced with Tiger.)
  2. Stefan’s and mine give the correct results when run on Friday or Saturday.
  3. Vincent’s is the fastest.

I think the mathematics for Vincent’s script should be:

set theDate to (current date)
set NextFriday to theDate + ((12 - (weekday of theDate) as integer) mod 7 + 1) * 86400 -- or: * days

On my machine, an automatic weekday → integer coercion takes place if none is specified and this would make Vincent’s idea (and Stefan’s) even faster.

set theDate to (current date)
set NextFriday to theDate + ((12 - (weekday of theDate)) mod 7 + 1) * 86400

If the first line of Craig’s script is changed to ‘set theDate to (current date) + days’, his script too will give “the next Friday after the current date” when it’s run on a Friday.

Hi Geddy,

This always happens when someone posts date problems. :slight_smile:

If you don’t want a lot of scripts, the easiest to remember is Craig’s. You just increment the date until the weekday of the date is Friday. For one iteration, the difference in speed between scripts is negligable. For a thousand iterations, the difference on my machine is about 1/30 of a second. If you’re getting many dates, you want to split up getting the current date from the rest of your script as in Vincent’s and Craig’s, because most of the time is spent getting the current date I think. Nigel’s is always the fastest.

gl,

Oh, you’re right. I simply forgot to test what happens if the current date is a Friday or Sunday. I just checked if the result is a Friday at all. My bad :confused: :mad:

And thank you for the info on compiled localized dates! You never stop learning :cool:

Wow! I hadn’t checked this post in bit, because I was just using the multiple if statements that I had suggested first. That works perfectly for my purposes, but I’m sure its not at all elegant. I’m going to take a look at all these suggestions today. Thanks guys.

Mike