Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

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
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you