Tuesday, 5 May 2015

Automation Script - get difference between dates

If you want to get a difference between two dates in Maximo automation scripts, I tried below process:

For example:
In Work Order, you want to find a days between Target Start Date and Target Finish Date.

Target Start Date is "01/Apr/2015"
Target Finish Date is "25/Apr/2015"
Difference from Target Start Date and Target Finish Date is 25

Lets see how to write automation script:

Object: WORKORDER

Varibles declared as:

tsd = Target Start Date
tfd = Target Finish Date
dayselapsed = CUSTOM_DAYSELAPSED (Custom attribute to set days - integer)

Script as follows:

#************** AUTO SCRIPT START ***************
#Define a function to get date difference
#************** FUNCTION START ***************
def getDateDiff(timeDiff):

#Seconds in Millis
 milliseconds = 1000

#Minutes in Millis
 milliminutes = milliseconds * 60

#Hours in Millis
 millihours = milliminutes * 60

#Days in Millis
 millidays = millihours * 24

#Get days diff
daysDiff = timeDiff / millidays

#Get hours diff
hoursDiff = timeDiff/millihours

#Get minutes diff
minDiff = timeDiff / milliminutes


#Return days difference
if(daysDiff != 0):
 return int(daysDiff)

#Return hours difference
if(hoursDiff != 0):
 return int(hoursDiff)

#Return minutes difference
if(minDiff != 0):
 return int(minDiff )

#************** FUNCTION END ***************

#Time difference between two dates
timeDiff = tsd.getTime() - tfd.getTime()
daysDiffernce = getDateDiff(timeDiff)
dayselapsed = daysDifference

#************** AUTO SCRIPT END ***************

For few developers, if above script confuses you, below script without comments, to return days difference:

#************** AUTO SCRIPT START ***************
#Define a function to get date difference
#************** FUNCTION START ***************

def getDateDiff(timeDiff):
 milliseconds = 1000
 milliminutes = milliseconds * 60
 millihours = milliminutes * 60
 millidays = millihours * 24

daysDiff = timeDiff / millidays
hoursDiff = timeDiff/millihours
minDiff = timeDiff / milliminutes

if(daysDiff != 0):
 return int(daysDiff)

#************** FUNCTION END ***************

timeDiff = tsd.getTime() - tfd.getTime()
daysDiffernce = getDateDiff(timeDiff)
dayselapsed = daysDifference

#************** AUTO SCRIPT END ***************

 

No comments:

Maximo SOAPUI error javax.xml.ws.WebServiceException

SOAPUI error while sending payload to below url (in cluster environment) http://localhost:9080/meaweb/services/MXASSET Error : <f...