Jump to content

else if returning a date


n2_space

Recommended Posts

I'm trying to make a rule return a date a certain number of days out based on what day it is. Mon-Wed it will be two days from the date composed, and thursday and friday will be 4 days out.

 

var theDate=new Date(Today());
var weekday=new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";
var newDate = weekday[theDate.getDay()]; 


var today = new Date(); 
var tomorrow = new Date(); 
if (weekday = 0) tomorrow.setDate(today.getDate()+2);
else if (weekday = 1) tomorrow.setDate(today.getDate()+2);
else if (weekday = 2) tomorrow.setDate(today.getDate()+2);
else if (weekday = 3) tomorrow.setDate(today.getDate()+2);
else if (weekday = 4) tomorrow.setDate(today.getDate()+4);
else if (weekday = 5) tomorrow.setDate(today.getDate()+4);
else if (weekday = 6) tomorrow.setDate(today.getDate()+2);

return FormatDate(tomorrow, "m/d/yyyy"); 

 

Apparently I have an issue as I test it by saying today is +5 just to test (instead of +2 for example for today as weekday 2), I am just getting +2... what am I missing? :confused:

Edited by n2_space
Link to comment
Share on other sites

I'm trying to make a rule return a date a certain number of days out based on what day it is. Mon-Wed it will be two days from the date composed, and thursday and friday will be 4 days out.

What if it's Saturday or Sunday?

 

var theDate=new Date(Today());
var weekday=new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";
var newDate = weekday[theDate.getDay()]; 


var today = new Date(); 
var tomorrow = new Date(); 
if (weekday = 0) tomorrow.setDate(today.getDate()+2);
else if (weekday = 1) tomorrow.setDate(today.getDate()+2);
else if (weekday = 2) tomorrow.setDate(today.getDate()+2);
else if (weekday = 3) tomorrow.setDate(today.getDate()+2);
else if (weekday = 4) tomorrow.setDate(today.getDate()+4);
else if (weekday = 5) tomorrow.setDate(today.getDate()+4);
else if (weekday = 6) tomorrow.setDate(today.getDate()+2);

return FormatDate(tomorrow, "m/d/yyyy"); 

Apparently I have an issue as I test it by saying today is +5 just to test (instead of +2 for example for today as weekday 2), I am just getting +2... what am I missing? :confused:

For some reason, which I don't understand, you're going through the trouble to map the number representing the day of the week to a string. Then you're getting a string value from your array, but you're trying to compare that string value to a number. You need to compare the string to a string again if your'e going to do that. But that all seems like a lot of extra work and room for error.

 

It seems much simpler to me to just compare the number returned from Date.getDay() as a number. (Besides, if you want a string representing the name of the day of the week for a particular date, you don't need to make an array of day names; you can just call FormatDate() with "ld" as the format.)

 

Also, all those nested "else if"s just scream out for a switch statement.

 

Try this:

var myDate = Today();
var daysToAdd = 2;
switch (myDate.getDay())
{
   case 0: // Sunday
   case 6: // Saturday
       throw "Not valid on weekends";
   case 4: // Thursday
   case 5: // Friday
       daysToAdd = 4;
}
myDate.setDate(myDate.getDate() + daysToAdd);
return FormatDate(myDate, "m/d/yyyy");

Edited by Dan Korn
typo
Link to comment
Share on other sites

What if it's Saturday or Sunday?

 

For some reason, which I don't understand, you're going through the trouble to map the number representing the day of the week to a string. Then you're getting a string value from your array, but you're trying to compare that string value to a number. You need to compare the string to a string again if your'e going to do that. But that all seems like a lot of extra work and room for error.

 

It seems much simpler to me to just compare the number returned from Date.getDay() as a number. (Besides, if you want a string representing the name of the day of the week for a particular date, you don't need to make an array of day names; you can just call FormatDate() with "ld" as the format.)

 

Also, all those nested "else if"s just scream out for a switch statement.

 

Try this:

var myDate = Today();
var daysToAdd = 2;
switch (myDate.getDay())
{
   case 0: // Sunday
   case 6: // Saturday
       throw "Not valid on weekends";
   case 4: // Thursday
   case 5: // Friday
       daysToAdd = 4;
}
myDate.setDate(myDate.getDate() + daysToAdd);
return FormatDate(myDate, "m/d/yyyy");

 

Saturday and Sunday should be 2 days out, at least right now... we don't currently have any that will happen then, but was trying to plan for that scenario.

 

That worked beautifully, thank you! I am still very much a newbie to javascript and tried to look at other examples on the forum :o

Link to comment
Share on other sites

Saturday and Sunday should be 2 days out, at least right now... we don't currently have any that will happen then, but was trying to plan for that scenario.

Okay, then you can just remove the two "case" lines for those days and the following "throw" line.

That worked beautifully, thank you! I am still very much a newbie to javascript and tried to look at other examples on the forum :o

Sure, no problem. You're doing pretty well, I think.

Edited by Dan Korn
mangled the [QUOTE] tags
Link to comment
Share on other sites

Okay, then you can just remove the two "case" lines for those days and the following "throw" line.

 

Sure, no problem. You're doing pretty well, I think.

Thanks! I have a couple others that are similiar (only on Friday and 3 days out then), so I think I can figure out how to change the code enough to make those work (I hope! :) )

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...