Abstract
This code snippet adds a certain number of days to a date excluding the weekends. Currently, when you add a certain number of days to a date the weekends are included. To exclude them, you can use this snippet.
...
- Fetch the number of days to be added and first add the whole weeks
- Before adding the remaining days, skip the start if it is a weekendRun a loop to add a day each until the number of remaining days - Skip if the day is a "Saturday" or a "Sunday"
- After adding the days, skip the end if it is a weekend
- Return the Momentjs
...
Code Block | ||||
---|---|---|---|---|
| ||||
{% macro add(from, nod) %} {% set weeks = nod/5 %} {% set weeks = weeks | int %} {% set remDays = nod%5 %} {# Adding the whole weeks #} {% set from = from | date('add', weeks,'weeks') %} {# Skipping the weekend if starting with Friday Run a loop and check each day to skip a weekend #} {% iffor fromi | date('day') == "5" and remDays > 0 %} in remDays %} {% set from = from | date('add',1,'d') | date %} {% endif %} {# Run a loop and check each {#day Skippingto theskip startinga weekend #} {% if from | date('day') == "6" %} {% set from = from | date('add',1,'d') %} {% endif %} {% if from | date('day') == "0" and remDays == 0 %} {% set from = from | date('add',1,'d') %} {% endif| date %} {# Add remaining days #} {% setendif %} from = from | date('add',remDays,'d'){% endfor %} {# Skipping the ending weekend #} {% if from | date('day') == "6" %} {% set from = from | date('add',1,'d') %} {% endif %} {% if from | date('day') == "0" %} {% set from = from | date('add',1,'d') %} {% endif %} {#Return the Momentjs#} {{caller()}} {{from | date}} {% endmacro%} {% call add(<from>, <nod>) -%} {%- endcall %} |
...
Placeholder | Description | Example |
---|---|---|
<from > | Date to which the number of days should be added | new Date() |
<nod> | Number of days to add | 22 |
Examples
The output of the code snippet is a Moment.js date object which you could use to:
- Set a Date/Date-time picker field - Eg: Set the Due date to issue created plus 5 days excluding weekends in
- one of the Set Field Value post-functions
in the Create issue post-function under Set fields of new issue section
Code Block language js linenumbers true {% macro add(from, nod) %} {% set weeks = nod/5 %} {% set weeks = weeks | int %} {% set remDays = nod%5 %} {# Adding the whole weeks #} {% set from = from | date('add', weeks,'weeks') %} {# Skipping the weekend if starting with Friday Run a loop and check each day to skip a weekend #} {% iffor fromi | date('day') == "5" and remDays > 0 %} in remDays %} {% set from = from | date('add',1,'d') | date %} {% endif %} {# Run a loop and check each {#day Skippingto theskip startinga weekend #} {% if from | date('day') == "6" %} {% set from = from | date('add',1,'d') %} {% endif %} {% if from | date('day') == "0" and remDays == 0 %} {% set from = from | date('add',1,'d') %} {% endif | date %} {# Add remaining days #} {% setendif %} from = from | date('add',remDays,'d'){% endfor %} {# Skipping the ending weekend #} {% if from | date('day') == "6" %} {% set from = from | date('add',1,'d') %} {% endif %} {% if from | date('day') == "0" %} {% set from = from | date('add',1,'d') %} {% endif %} {#Return the Momentjs#} {{caller()}} {{from | date}} {% endmacro%} {% call add(issue.fields.created, 5) -%} {%- endcall %}
- Notify the customer that the issue will be resolved in 6 days from today through the
- Comment in one of the Comment issue post-functions
Subject/HTML body/Text body of Email issue post-function
Code Block language js linenumbers true {% macro add(from, nod) %} {% set weeks = nod/5 %} {% set weeks = weeks | int %} {% set remDays = nod%5 %} {# Adding the whole weeks #} {% set from = from | date('add', weeks,'weeks') %} {# Skipping the weekend if starting with Friday Run a loop and check each day to skip a weekend #} {% iffor from | date('day') == "5" and remDays > 0i in remDays %} {% set from = from | date('add',1,'d') | date %} {% endif %}{# Run a loop and check each day to {# Skipping the startingskip a weekend #} {% if from | date('day') == "6" %} {% set from = from | date('add',1,'d') %} {% endif %} {% if from | date('day') == "0" and remDays == 0 %} {% set from = from | date('add',1,'d') %} {% endif %} {# Add remaining days #} {% set from = from | date('add',remDays,'d')endfor %} {# Skipping the ending weekend #} {% if from | date('day') == "6" %} {% set from = from | date('add',1,'d') %} {% endif %} {% if from | date('day') == "0" %} {% set from = from | date('add',1,'d') %} {% endif %} {#Return the Momentjs#} {{ caller() }} {{from | date}} {% endmacro%} {% call add(issue.fields.creatednow, 56) -%} Your issue will be resolved on/ or before: {%- endcall %}{% macro add(from, nod) %}
References
- issue variable
- getLinkedIssues()
- Accessing the fields of an issue
- Groovy DocumentationHow to insert information using Nunjucks annotations
- Accessing JIRA Standard fields
- now variable
- Date filter
Related articles
Filter by label (Content by label) | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Page Properties | ||
---|---|---|
| ||
|