Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

...

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 weekend
  • After adding the days, skip the end if it is a weekend
  • Return the Momentjs

Snippet

Code Block
languagejs
linenumberstrue
{% macro add(from, nod) %}
	{% set weeks = nod/5 %}
	{% set weeks = weeks | int %}
	{% set remDays = nod%5 %}

	{# Adding the whole weeks #}
	{% set from = from <fromDate> | date('addbusinessAdd', weeks,'weeks') %}

	{# Skipping the weekend if starting with Friday #}
    {% if from | date('day') == "5" and remDays > 0 %}
		{% set from = from | date('add',1,'d'<nod> ) | date %}
	{% endif %}
    
    {# Skipping the starting 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') %}

	{# 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 %}

	{{from | date}}
{% endmacro%}

{% call add(<from>, <nod>) -%}
{%- endcall %}

Placeholders

Placeholder

Description

Example

<from<fromDate>Date to which the number of days should be addednew Date()now
<nod>Number of days to add22


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
      languagejs
      linenumberstrue
      {% 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 #}
          {% if from | date('day') == "5" and remDays > 0 %}
      		{% set from = from | date('add',1,'d') | date %}
      	{% endif %}
          
          {# Skipping the starting 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') %}
      
      	{# 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 %}
      
      	{{from | date}}
      {% endmacro%}
      
      {% call add(issue.fields.created, 5) -%}
      {%- endcall %} issue.fields.duedate | date('businessAdd', 5 ) | date }}
      


  • 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
      languagejs
      linenumberstrue
      {%Your macro add(from, nod) %}
      	{% set weeks = nod/5 %}
      	{% set weeks = weeks | int %}
      	{% set remDays = nod%5 %}
      
      	{# Adding the whole weeks #}
      	{% set from = from issue will be resolved on or before: {{ issue.fields.created | date('addbusinessAdd', weeks,'weeks') %}
      
      	{# Skipping the weekend if starting with Friday #}
          {% if from | date('day') == "5" and remDays > 0 %}
      		{% set from = from | date('add',1,'d') | date %}
      	{% endif %}
          
          {# Skipping the starting 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') %}
      
      	{# 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 %}
      
      	{{from | date}}
      {% endmacro%}
      
      {% call add(issue.fields.created, 5) -%}
      Your issue will be resolved on/before 
      {%- endcall %}

A Groovy expression, for example to:

Set a Date/Date-time picker field - Eg: Set a Date-time picker field to 5 days plus the Due date in

  • one of the Set Field Value post-functions
  • the Create issue post-function under Set fields of new issue section

Code Block
date(issue.get("duedate"),5)

A Groovy template, for example to:

Notify the customer that the issue will be resolved in ten days excluding weekends, through the
  • Comment in one of the Comment issue post-functions
  • Subject/HTML body/Text body of Email issue post-function

    Code Block
    <% Date date(Date from, int nod){
        Calendar c1 = GregorianCalendar.getInstance();
        c1.setTime(from);
    
        int weeks = nod/5;
        int remDays = nod%5;
    
        //Adding whole weeks
        c1.add(Calendar.WEEK_OF_YEAR, weeks);
    
        //Skipping starting weekend
        if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY)
            c1.add(Calendar.DAY_OF_MONTH, 1);
        if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
            c1.add(Calendar.DAY_OF_MONTH, 1);
    
        //Add days
        c1.add(Calendar.DAY_OF_MONTH, remDays);
    
        //Skip ending weekend
        if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY)
            c1.add(Calendar.DAY_OF_MONTH, 1);
        if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
            c1.add(Calendar.DAY_OF_MONTH, 1);
    
        //move to 0:00
        c1.clearTime();
        
        return c1.getTime();
    }
    %>
    Your issue will be resolved on or before <%=date(new Date(),10)%>

    References

    ...

      • }


    References

    Filter by label (Content by label)
    showLabelsfalse
    max5
    spacesKB
    showSpacefalse
    sortmodified
    reversetrue
    typepage
    cqllabel = "jmwe-nunjucks" and type = "page" and space = "JMWEC"
    labelsjmwe-nunjucks jmwe-cloud

    Page Properties
    hiddentrue


    Related issues