PDA

View Full Version : else if returning a date


n2_space
April 16th, 2013, 08:39 AM
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:

Dan Korn
April 16th, 2013, 09:33 AM
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() (https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/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");

n2_space
April 16th, 2013, 09:38 AM
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() (https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/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

Dan Korn
April 16th, 2013, 09:44 AM
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.

n2_space
April 16th, 2013, 10:26 AM
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! :) )