PrintNW Posted October 30, 2013 Share Posted October 30, 2013 I need some help with a complex formula. I need to create a rule that will grab the current date and time, add 24 hours to it, and then return a date value that is either one day later (if the current time is earlier than 10:00 AM) or two days later (if the current time is later than 10:00 AM). In other words, I want to display a due date that is 12:00 noon of the next day if the user is placing an order before 10:00 am, but displays a due date of 2 days later, at 12:00 noon, if the user is placing an order after 10:00 am. By the way, there aren't any fields being referenced. I'm strictly using the getDate or getTime feature. I won't bother posting all my various rule attempts. But I've played with converting and comparing the date to milliseconds, looked at samples of setting future due dates found here on the message boards and various formatting options. Suffice it to say that I'm now completely lost - lol. Can you please help? Quote Link to comment Share on other sites More sharing options...
Dan Korn Posted October 30, 2013 Share Posted October 30, 2013 I need some help with a complex formula. I need to create a rule that will grab the current date and time, add 24 hours to it, and then return a date value that is either one day later (if the current time is earlier than 10:00 AM) or two days later (if the current time is later than 10:00 AM). In other words, I want to display a due date that is 12:00 noon of the next day if the user is placing an order before 10:00 am, but displays a due date of 2 days later, at 12:00 noon, if the user is placing an order after 10:00 am. By the way, there aren't any fields being referenced. I'm strictly using the getDate or getTime feature. I won't bother posting all my various rule attempts. But I've played with converting and comparing the date to milliseconds, looked at samples of setting future due dates found here on the message boards and various formatting options. Suffice it to say that I'm now completely lost - lol. Can you please help? Well, first, I should offer this caveat: Unless you're only doing local compositions in FusionPro VDP Creator (Desktop), in Acrobat, on your own local machine, the time may be different on whatever other computer is actually doing the composition, in a FusionPro VDP Producer (Direct) or Producer API (Server) system, or any web-to-print system such as MarcomCentral or DSF. So be aware of that. That said, this should work: var myDate = new Date(); // current local date and time myDate.setDate(myDate.getDate()+1); // add one day if (myDate.getHours() >= 10) // if after 10 am myDate.setDate(myDate.getDate()+1); // add another day myDate.setHours(12); myDate.setMinutes(0); myDate.setSeconds(0); // set to noon return myDate;Or, you can replace the last two lines with something like this: return "noon on " + FormatDate(myDate, "ld, lm d, yyyy"); Quote Link to comment Share on other sites More sharing options...
PrintNW Posted October 30, 2013 Author Share Posted October 30, 2013 Okay, this works great, however, now that I look at it I realize that I made this too simple (of course, right). We are not open on Saturday and Sunday, so the days added need to be adjusted based on the day the item is ordered. I played with variations of the following code, but it still doesn't work correctly. Basically... All orders placed Thurs after 10 are due Mon at noon. All orders placed Fri before 10 are due Mon at noon. All orders placed Fri after 10 are due Tue at noon. All orders placed Sat or Sun (at any time) are due Tue at noon. All other days and times foilow the old formula Here is my last attempt: var myDate = new Date(); // current local date and time myDate.setDate(myDate.getDate()+1); // add one day var theDate=new Date(Today()); var weekday=new Array(7); weekday[0]="4"; weekday[1]="0"; weekday[2]="0"; weekday[3]="0"; weekday[4]="1"; weekday[5]="2"; weekday[6]="3"; var newDay = weekday[theDate.getDay()]; if ((newDay=0) && (myDate.getHours() >= 10)) myDate.setDate(myDate.getDate()+1); else if ((newDay=1) && (myDate.getHours() >= 10)) myDate.setDate(myDate.getDate()+4); else if ((newDay=2) && (myDate.getHours() <= 10)) myDate.setDate(myDate.getDate()+3); else if ((newDay=2) && (myDate.getHours() >= 10)) myDate.setDate(myDate.getDate()+4); else if (newDay=3) myDate.setDate(myDate.getDate()+3); else if (newDay=4) myDate.setDate(myDate.getDate()+2); else myDate.setDate(myDate.getDate()); myDate.setHours(12); myDate.setMinutes(0); myDate.setSeconds(0); // set to noon return FormatDate (myDate,"mm/dd/yy - hh:nn")+" Noon"; Any ideas? Quote Link to comment Share on other sites More sharing options...
PrintNW Posted October 31, 2013 Author Share Posted October 31, 2013 I've been playing around with different variations of the rule, but still can't get it to do what I need. Have you had any luck? Quote Link to comment Share on other sites More sharing options...
Dan Korn Posted October 31, 2013 Share Posted October 31, 2013 Okay, this works great, however, now that I look at it I realize that I made this too simple (of course, right). Ah, the old "changing requirements after the code is delivered" trick! That never happens. It's not a big deal, though. We are not open on Saturday and Sunday, so the days added need to be adjusted based on the day the item is ordered. I played with variations of the following code, but it still doesn't work correctly. Basically... All orders placed Thurs after 10 are due Mon at noon. All orders placed Fri before 10 are due Mon at noon. All orders placed Fri after 10 are due Tue at noon. All orders placed Sat or Sun (at any time) are due Tue at noon. All other days and times foilow the old formula There are several approaches you could take here, but I took the route of turning your psuedocode requirements into functional code: var myDate = new Date(); // current local date and time // set to a particular date to test //myDate = new Date(2013, 11 - 1, 1, 11); // Nov. 1, 2013, 11 am var after10am = myDate.getHours() >= 10; // true if after 10 am var before10am = !after10am; // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay var currentDay = myDate.getDay(); // 0 = Sunday, 1 = Monday, etc., to 6 = Saturday //return [currentDay, after10am]; // debugging var daysToAdd = 1; //All orders placed Thurs after 10 are due Mon at noon. if (currentDay == 4 && after10am) daysToAdd = 4; //All orders placed Fri before 10 are due Mon at noon. else if (currentDay == 5 && before10am) daysToAdd = 3; //All orders placed Fri after 10 are due Tue at noon. else if (currentDay == 5 && after10am) daysToAdd = 4; //All orders placed Sat or Sun (at any time) are due Tue at noon. else if (currentDay == 6) // Sat daysToAdd = 3; else if (currentDay == 0) // Sun daysToAdd = 2; //All other days and times foilow the old formula: // 12:00 noon of the next day if the user is placing an order before 10:00 am, else if (before10am) daysToAdd = 1; // but 2 days later, at 12:00 noon, if the user is placing an order after 10:00 am. else // if (after10am) daysToAdd = 2; // add the calculated number of days myDate.setDate(myDate.getDate() + daysToAdd); //return "noon on " + FormatDate(myDate, "ld, lm d, yyyy"); // and set to noon myDate.setHours(12); myDate.setMinutes(0); myDate.setSeconds(0); return myDate; Quote Link to comment Share on other sites More sharing options...
PrintNW Posted October 31, 2013 Author Share Posted October 31, 2013 Brilliant. I understand the direction you went and it works perfectly. Thank you so much. Okay, you have my permission to take the rest of the day off. Quote Link to comment Share on other sites More sharing options...
Dan Korn Posted October 31, 2013 Share Posted October 31, 2013 Okay, you have my permission to take the rest of the day off. Thanks! if (PrintNW.allowsLeaveEarly() && new Date().getHours() >= 15) Dan.Go("home"); Here's an update to the rule, where the "if" logic is a bit easier to read, with the actual day names: var myDate = new Date(); // current local date and time // set to a particular date to test //myDate = new Date(2013, 11 - 1, 1, 11); // Nov. 1, 2013, 11 am var after10am = myDate.getHours() >= 10; // true if after 10 am var before10am = !after10am; // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay var DaysOfWeek = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]; var currentDayIndex = myDate.getDay(); // 0 = Sunday, 1 = Monday, etc., to 6 = Saturday var currentDay = DaysOfWeek[currentDayIndex]; //return [currentDay, after10am]; // debugging var dueDay = ""; //All orders placed Thurs after 10 are due Mon at noon. if (currentDay == "Thursday" && after10am) dueDay = "Monday"; //All orders placed Fri before 10 are due Mon at noon. else if (currentDay == "Friday" && before10am) dueDay = "Monday"; //All orders placed Fri after 10 are due Tue at noon. else if (currentDay == "Friday" && after10am) dueDay = "Tuesday"; //All orders placed Sat or Sun (at any time) are due Tue at noon. else if (currentDay == "Saturday" || currentDay == "Sunday") dueDay = "Tuesday"; //All other days and times foilow the old formula: // 12:00 noon of the next day if the user is placing an order before 10:00 am, else if (before10am) dueDay = DaysOfWeek[currentDayIndex + 1]; // but 2 days later, at 12:00 noon, if the user is placing an order after 10:00 am. else // if (after10am) dueDay = DaysOfWeek[currentDayIndex + 2]; // add days until we get to the right due day while (DaysOfWeek[myDate.getDay()] != dueDay) { myDate.setDate(myDate.getDate() + 1); // add one day } //return "noon on " + FormatDate(myDate, "ld, lm d, yyyy"); // and set to noon myDate.setHours(12); myDate.setMinutes(0); myDate.setSeconds(0); return myDate; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.