cancel
Showing results for
Did you mean:
Highlighted
Nintex Newbie

## Need to calculate the current day to find the next Tuesday

So I have a situation where the user inputs how long a job posting will be listed.  He/she will have the choice of 1, 2, or 4 weeks.  I'd like to then display the date this will correspond to.  Now, we post only on Tuesdays.  Therefore, the last day (the closing date) will always be on a Tuesday.  I'm guessing this will have to be accomplished by JavaScript which I know little to nothing about.  Hopefully someone already has something already created they would be willing to share.

8 Replies
Nintex Newbie

## Re: Need to calculate the current day to find the next Tuesday

It doesn't use JavaScript and might get you started on the right path.

Nintex Newbie

## Re: Need to calculate the current day to find the next Tuesday

Ok, this starts it.  I have it telling me what day is today. How would I then take that to count days to find the next Tuesday?

Nintex Newbie

## Re: Need to calculate the current day to find the next Tuesday

I'm sorry I'm not going to have very much time this week to look into this more, but I did spend a little time to find and tweak some JavaScript that might help you. It's not going to work to just copy and paste into your solution, but I'm sure there are others who could help finish it.

I found this StackOverflow article, which led me to this JSFiddle by Patito, which I tweaked into this solution.

``var today = new Date();var result = document.getElementById('result');result.innerHTML += "Today: <b>" + today + "</b><br/>";result.innerHTML += "Next Tuesday: <b>" + nextDay(today, 2) + "</b><br/>";result.innerHTML += "The Tuesday After That: <b>" + nextDay(today, 2, 1) + "</b><br/>";result.innerHTML += "The Tuesday After That: <b>" + nextDay(today, 2, 2) + "</b><br/>";result.innerHTML += "The Tuesday After That: <b>" + nextDay(today, 2, 3) + "</b><br/>";result.innerHTML += "The Tuesday After That: <b>" + nextDay(today, 2, 4) + "</b><br/>";function nextDay(today, dayOfWeek, weeks = 0) {  var returnDate = new Date();  returnDate.setDate(today.getDate() + (weeks * 7) + (dayOfWeek + (7 - today.getDay())) % 7);  return returnDate;}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍``

I know you said you don't have very much experience with JavaScript so, I'm going to try and explain what's happening here.

Lines 4-8 are just duplicates with the week number incremented in the nextDay function. This should show you how to select a specific number of weeks into the future.

In this example, document.getElementByID is being used to update the innerHTML text. You'll most likely need to modify this for your specific solution.

The key to this code is the nextDay() function. It accepts a date value which is set to today (another date could be used though), an integer as the day of the week (0-6 which is Sun-Sat), and an optional integer variable for the number of weeks into the future.

You can see that I'm calling this function in lines 4-8 and I send it today (the current date), 2 (for Tuesday), and a number to represent the number of weeks into the future I want (you do not need to enter 0 for the current week). You should even be able to enter negative numbers to retrieve weeks into the past.

``returnDate.setDate(today.getDate() + (weeks * 7) + (dayOfWeek + (7 - today.getDay())) % 7);‍``

This line of code is doing all of the calculation work. It's setting the returnDate variable to the calculated date and returning it on the next line.

today.getDate() returns the current date number (i.e. Aug 20th returns 20)

This is added to the number of weeks from today multiplied by 7 (days in a week)

That result is added to the next section, which can be a little confusing:

- dayOfWeek is the numeric value of the day we want (2 = Tues) which is added to

- 7 (days in week) minus

- today.getDay() returns today's numeric day value

- The last three lines are then used to find the remainder (modulus) of 7 (days in a week)

(modulus or remainder simply means the remainder after a number is divided by another)

Today is Monday, Aug 20th so, for next Tues, the equation would look like this:

20 + (0) + (2 + (7 - 1)) % 7 = 21, meaning that the next Tuesday is tomorrow, Aug 21st.

And don't worry if the number is greater than the number of days in the month. The .setDate() function automatically calculates the date into the next month. No extra calculations are needed.

I hope this is helpful. If I do get any extra time, I'll try to mock it up into a Nintex Form and post it. And if you get it figured out, please post it here as well so it can help others.

Nintex Newbie

## Re: Need to calculate the current day to find the next Tuesday

Thank you Chad for looking into this and your detailed description. Unfortunately it is starting to dive into a world I know very little of.  I was hoping there would be something out there that did this calculation already or some posts that did this.  My form really doesn't have the requirement to show the specific date, I was just doing it to clarify things on my form.  If I have some additional time, I may look a bit further into this.  Thank you again.

Automation Master

## Re: Need to calculate the current day to find the next Tuesday

javascript is not needed at all, it can be calculated with calculated value control and provided runtime functions.

try

``dateAddDays(    dateAddDays(StartDate,7*Weeks),    1*replace(        replace(            replace(                replace(                    replace(                        replace(                            replace(                                formatDate(                                    dateAddDays(StartDate,7*Weeks),"ddd"                                ),"Mon",1                            ),"Tue",0                        ),"Wed",6                    ),"Thu",5                ),"Fri",4            ),"Sat",3        ),"Sun",2    ))‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍``
Nintex Newbie

## Re: Need to calculate the current day to find the next Tuesday

Not sure if I’ve typed in the formula wrong, but since I created a calculated value and attempted this formula the form doesn’t show up in preview mode. I removed the control and it came back ok. I thought it might have been the value “StartDate” so I removed that value and inserted just Current Date. That didn’t help things. Here is the formula I have. Maybe I’m missing a parathesis somewhere?

Brian Knight

Director of Information Technology

Hamilton County Developmental Disabilities Services

W: 513-559-6678

C: 859-250-5819

Automation Master

## Re: Need to calculate the current day to find the next Tuesday

I'm not sure, whether it's just copy/paste problem but I noticed a few syntactical problems

- you miss an asterix signs in "7Weeks" and "1replace"

- you miss a closing apostrophee after "Wed"

note StartDate and Weeks are meant as named control or item property references, so you have to replace them after you copy&paste formula

Nintex Newbie