Go Back   MarcomCentral (PTI) and FusionPro User Communities > Software-Related Talk > The JavaScript Library > Dates

Notices

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old April 16th, 2013, 08:39 AM
n2_space n2_space is offline
Regular Contributor
 
Join Date: Jul 2011
Posts: 53
Default else if returning a date

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.

Code:
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?
__________________
9.0.3, Acrobat X

Last edited by n2_space; April 16th, 2013 at 08:41 AM..
Reply With Quote
  #2  
Old April 16th, 2013, 09:33 AM
Dan Korn's Avatar
Dan Korn Dan Korn is offline
FusionPro Senior Engineer / Forum Moderator
 
Join Date: Aug 2008
Location: Chicago, IL
Posts: 4,305
Default Re: else if returning a date

Quote:
Originally Posted by n2_space View Post
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?
Quote:
Originally Posted by n2_space View Post

Code:
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?
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:
Code:
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");
__________________
Dan Korn
FusionPro Developer / JavaScript Guru / Forum Moderator
PTI Marketing Technologies | Printable | MarcomCentral
LinkedIn

I am a not a Support engineer, and this forum is not a substitute for Support. My participation on this forum is primarily as a fellow user (and a forum moderator). I am happy to provide help and answers to questions when I can; however, there is no guarantee that I, or anyone else on this forum, will be able to answer all questions or fix any problems. If I ask for files to clarify an issue, I might not be able to look at them personally. I am not able to answer private messages, emails, or phone calls unless they go through proper Support channels. Please direct any sales or pricing questions to your salesperson or inquiries@marcom.com.

Complex template-building questions, as well as all installation and font questions or problems, should be directed to FusionProSupport@marcom.com. Paid consulting work may be required to fulfill your template-building needs.

This is a publicly viewable forum. Please DO NOT post fonts, or other proprietary content, to this forum. Also, please DO NOT post any "live" data with real names, addresses, or any other personal, private, or confidential data.

Please include the specific versions of FusionPro, Acrobat, and your operating system in any problem reports or help requests. I recommend putting this information in your forum signature. Please also check your composition log (.msg) file for relevant error or warning messages.

Please post questions specific to the MarcomCentral Enterprise and Web-to-Print applications in the MarcomCentral forum. Click here to request access. Or contact your Business Relationship Manager (BRM/CPM) for assistance.

Please direct any questions specific to EFI's Digital StoreFront (DSF) to EFI support.

How To Ask Questions The Smart Way

The correct spellings are JavaScript, FusionPro, and MarcomCentral (each with two capital letters and no spaces). Acceptable abbreviations are JS, FP, and MC (or MCC). There is no "S" at the end of "Expression" or "Printable"! The name of the product is FusionPro, not "Fusion". "Java" is not is not the same as JavaScript.

Check out the JavaScript Guide and JavaScript Reference! FusionPro 8.0 and newer use JavaScript 1.7. Older versions use JavaScript 1.5.

return "KbwbTdsjqu!spdlt\"".replace(/./g,function(w){return String.fromCharCode(w.charCodeAt()-1)});

Last edited by Dan Korn; April 16th, 2013 at 09:34 AM.. Reason: typo
Reply With Quote
  #3  
Old April 16th, 2013, 09:38 AM
n2_space n2_space is offline
Regular Contributor
 
Join Date: Jul 2011
Posts: 53
Default Re: else if returning a date

Quote:
Originally Posted by Dan Korn View Post
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:
Code:
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
__________________
9.0.3, Acrobat X
Reply With Quote
  #4  
Old April 16th, 2013, 09:44 AM
Dan Korn's Avatar
Dan Korn Dan Korn is offline
FusionPro Senior Engineer / Forum Moderator
 
Join Date: Aug 2008
Location: Chicago, IL
Posts: 4,305
Default Re: else if returning a date

Quote:
Originally Posted by n2_space View Post
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.
Quote:
Originally Posted by n2_space View Post
That worked beautifully, thank you! I am still very much a newbie to javascript and tried to look at other examples on the forum
Sure, no problem. You're doing pretty well, I think.
__________________
Dan Korn
FusionPro Developer / JavaScript Guru / Forum Moderator
PTI Marketing Technologies | Printable | MarcomCentral
LinkedIn

I am a not a Support engineer, and this forum is not a substitute for Support. My participation on this forum is primarily as a fellow user (and a forum moderator). I am happy to provide help and answers to questions when I can; however, there is no guarantee that I, or anyone else on this forum, will be able to answer all questions or fix any problems. If I ask for files to clarify an issue, I might not be able to look at them personally. I am not able to answer private messages, emails, or phone calls unless they go through proper Support channels. Please direct any sales or pricing questions to your salesperson or inquiries@marcom.com.

Complex template-building questions, as well as all installation and font questions or problems, should be directed to FusionProSupport@marcom.com. Paid consulting work may be required to fulfill your template-building needs.

This is a publicly viewable forum. Please DO NOT post fonts, or other proprietary content, to this forum. Also, please DO NOT post any "live" data with real names, addresses, or any other personal, private, or confidential data.

Please include the specific versions of FusionPro, Acrobat, and your operating system in any problem reports or help requests. I recommend putting this information in your forum signature. Please also check your composition log (.msg) file for relevant error or warning messages.

Please post questions specific to the MarcomCentral Enterprise and Web-to-Print applications in the MarcomCentral forum. Click here to request access. Or contact your Business Relationship Manager (BRM/CPM) for assistance.

Please direct any questions specific to EFI's Digital StoreFront (DSF) to EFI support.

How To Ask Questions The Smart Way

The correct spellings are JavaScript, FusionPro, and MarcomCentral (each with two capital letters and no spaces). Acceptable abbreviations are JS, FP, and MC (or MCC). There is no "S" at the end of "Expression" or "Printable"! The name of the product is FusionPro, not "Fusion". "Java" is not is not the same as JavaScript.

Check out the JavaScript Guide and JavaScript Reference! FusionPro 8.0 and newer use JavaScript 1.7. Older versions use JavaScript 1.5.

return "KbwbTdsjqu!spdlt\"".replace(/./g,function(w){return String.fromCharCode(w.charCodeAt()-1)});

Last edited by Dan Korn; April 16th, 2013 at 09:45 AM.. Reason: mangled the [QUOTE] tags
Reply With Quote
  #5  
Old April 16th, 2013, 10:26 AM
n2_space n2_space is offline
Regular Contributor
 
Join Date: Jul 2011
Posts: 53
Default Re: else if returning a date

Quote:
Originally Posted by Dan Korn View Post
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! )
__________________
9.0.3, Acrobat X
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 11:30 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) 2011, PTI Marketing Technologies™, Inc.