Jump to content

Return a future date, skipping weekends


Kim

Recommended Posts

Hi all,

 

I'm trying to figure out if it's possible to return a date x number of days in the future, but if the result falls on Sat. or Sun., skip ahead to the next Monday.

 

I want to use this for a coupon expiration and I want to return text which reads: Expires Month-Day-Year.

 

thanks!

kim

Link to comment
Share on other sites

// Start out with the current date.
var myDate = new Date();

// Add the specified number of days.
var numDaysAhead = 30;
myDate.setDate(myDate.getDate() + numDaysAhead);

// If Saturday or Sunday, advance to the next Monday.
while (!(myDate.getDay() % 6))
 myDate.setDate(myDate.getDate() + 1);

return "Expires " + FormatDate(myDate, "m-d-yyyy");

Link to comment
Share on other sites

Obi-Dan, the javascript god, to the rescue as usual!

 

Thanks a lot, Dan! This is a beautiful thing.

Your code is always clean and elegant and great to learn from.

 

How does the Sat/Sun skip part work?

 

I would have expected something that had to have lines of logic with a combo for: if sat, add 2 or if sun, add 1.

 

It works perfectly, of course.

But you always have the cleanest and prettiest way of doing things, so if you have a chance to explain, I always appreciate learning.

 

Thanks very much!

kim

Link to comment
Share on other sites

Thanks a lot, Dan! This is a beautiful thing.

Your code is always clean and elegant and great to learn from.

Thanks! They don't call me a programmer for nothin'!

How does the Sat/Sun skip part work?

 

I would have expected something that had to have lines of logic with a combo for: if sat, add 2 or if sun, add 1.

 

It works perfectly, of course.

But you always have the cleanest and prettiest way of doing things, so if you have a chance to explain, I always appreciate learning.

Sure, I'll break this down, from the inside out.

while (!(myDate.getDay() % 6))
 myDate.setDate(myDate.getDate() + 1);

Let's start here:

myDate.getDay()

The Date.getDay function returns a number from 0 to 6, where 0=Sunday, 1=Monday, etc.

https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Date/getDay

So, we're looking for a value of either 0 (Sunday) or 6 (Saturday).

myDate.getDay() % 6

The modulus operator (%) returns the remainder (modulo, or "mod" in mathematical parlance) from an integer division. For example, 358 mod 100 = 58, i.e, there's 58 left over after the division 358/100. (In other words, if you divide 358 by 100, you get 3 with 58/100 left over.) It's a bit of an arithmetical gimmick, but it's very handy.

https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Arithmetic_Operators#.25_(Modulus)

http://en.wikipedia.org/wiki/Modulo_operation

Here, we're looking for the remainder of dividing the result of myDate.getDay() by 6. So, if the value of myDate.getDay() is 0 (zero), then the remainder (modulo) of dividing it by 6 (or by any other number) is zero. And if the value of myDate.getDay() is 6, then the remainder of dividing it by 6 is also zero. For any other number in the range (1 through 5, inclusive), the remainder of dividing it by 6 is that number, which is non-zero. This brings us to the next bit:

(!(myDate.getDay() % 6))

The "not" operator (!) is a logical operator, which converts its operand to a Boolean (true/false) value, and then negates it, so that true becomes false and false becomes true:

https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Logical_Operators

It just so happens that when a numeric value is converted to Boolean, zero is treated as false and anything else is treated as true. So, our value of zero (which we get if it's Saturday or Sunday) is treated as "false", which the not (!) operator converts to true; otherwise it's treated as "true", which the not (!) operator converts to false. So, the overall condition is true if and only if the date is a Saturday or Sunday.

while (!(myDate.getDay() % 6))

The while statement executes as long as the condition is true, which we now know it is as long as the date is a Saturday or Sunday.

https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Statements/while

So, this code gets executed as many times as necessary until the condition is false, that is, until the date is not Saturday or Sunday:

myDate.setDate(myDate.getDate() + 1);

The Date.setDate function sets the day of the month. In this case, we're simply adding one to advance the Date object to the next date on the calendar. For instance, if the original date was March 1, it would become March 2. JavaScript stores Date values internally as just a number which represents an amount of time since a fixed date, so when you add one, it automatically knows that if, say, the date is set to something like December 32, it knows you really mean January 1.

https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Date/setDate

So, now we can see what the whole thing does:

while (!(myDate.getDay() % 6))
 myDate.setDate(myDate.getDate() + 1);

Basically, this says: "As long as the date is either a Saturday or a Sunday, move to the next day." So, if the original date is Saturday, it advances one to Sunday, then advances again to Monday, then it's done. So, if the original date is Sunday, it advances to Monday, then it's done. If the original date is any other day (Monday through Friday), the condition is false to begin with and it doesn't do anything.

Link to comment
Share on other sites

Again, beautiful.

What a thoroughly clear and instructive lesson.

 

I know that modulus operator has come up before, hopefully I can get it to stick in my head this time. Quite useful.

 

I appreciate your help and willingness to teach. You're the best!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...