Add a certain number of hrs to an issue created time excluding the weekends and non-working hrs

subham patra January 28, 2021

How to add a certain number of hrs to an issue created time excluding the weekends and non-working hrs based on current user time zone to set a custom field values (datetime type) using a JMWE post function.

For example;

-If created time + 6hrs falls after 6pm on Friday, It should consider Monday 3pm 

-If created time + 6hrs falls after 6pm on any weekdays , It should consider Next day 3pm 

-If created time + 6hrs falls after before 9am on any week days, It should consider same day 3pm 

import org.joda.time.DateTimeZone
import org.joda.time.DateTime
import com.atlassian.jira.timezone.TimeZoneManager

def CreatedTime = issue.get("created") + 6 // advance by 6 hours from created time
def now = CreatedTime.DateTime(DateTimeZone.forTimeZone(getComponent(TimeZoneManager).getTimeZoneforUser(currentUser)))
if (now.dayOfWeek > 5 || now.dayOfWeek == 5 && now.hourOfDay >= 18)
now = now.plusDays(7).withDayOfWeek(1).withTime(15,0,0,0) //move to Monday 3pm
else if (now.hourOfDay >= 18)
now = now.withTime(15,0,0,0).plusDays(1) //move to next day at 3pm
else if (now.hourOfDay < 9)
now = now.withTime(15,0,0,0) //same day 3pm

return now.toDate()

 Somehow the code is unable to fetch created time with current time zone and use in if else conditions. Any help would be helpful. Thank you!  

1 answer

1 accepted

0 votes
Answer accepted
David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 28, 2021

Hi @subham patra ,

I can see at least 2 problems in your code:

  1. you are adding 6 to the created date. That adds 6 days, not hours
  2. you are not doing anything with CreatedTime, you're creating a "now" variable instead that represents the current date/time - and in reality, that line doesn't even work, as Timestamp objects don't have a DateTime method...

Something like this is probably what you want:

import org.joda.time.DateTimeZone
import org.joda.time.DateTime
import com.atlassian.jira.timezone.TimeZoneManager

def created = new DateTime(issue.get("created"),DateTimeZone.forTimeZone(getComponent(TimeZoneManager).getTimeZoneforUser(currentUser)))
def now = created.plusHours(6)

if (now.dayOfWeek > 5 || now.dayOfWeek == 5 && now.hourOfDay >= 18)
now = now.plusDays(7).withDayOfWeek(1).withTime(15,0,0,0) //move to Monday 3pm
else if (now.hourOfDay >= 18)
now = now.withTime(15,0,0,0).plusDays(1) //move to next day at 3pm
else if (now.hourOfDay < 9)
now = now.withTime(15,0,0,0) //same day 3pm

return now.toDate()

Suggest an answer

Log in or Sign up to answer