Using date and time with scripts

silver link bracelet black u boat chronograph watch

There are several methods or functions that allow us to work with the date-time data type from a script. To do this, you can use Java native methods by importing the java.util.Date, java.util.Calendar classes, or you can also import a Maximo class psdi.app.common.DateUtility.

Get the date with hour/minute/second/millisecond stripped (00:00 midnight)

from psdi.app.common import DateUtility

date_midnigth = DateUtility.getDate(date1)
####
# date1:         2023-04-17 14:36:52
# date_midnigth: 2023-04-17 00:00:00

Add/subtract a number of days to/from a date

from psdi.app.common import DateUtility

newdate1 = DateUtility.addDays(today, 10)  # add 10 days
newdate2 = DateUtility.addDays(today, -5)  # subtract 5 days
####
# today:    2023-04-17 14:36:52
# newdate1: 2023-04-27 14:36:52
# newdate2: 2023-04-12 14:36:52

Add/subtract a number of minutes to/from a date

from psdi.app.common import DateUtility

newdate1 = DateUtility.addMinutes(today, 30)  # add 30 minutes
newdate2 = DateUtility.addMinutes(today, -10)  # subtract 10 minutes
####
# today:    2023-04-17 14:36:52
# newdate1: 2023-04-17 15:06:52
# newdate2: 2023-04-17 14:26:52

Add/subtract a number of seconds to/from a date

from psdi.app.common import DateUtility

newdate1 = DateUtility.addSeconds(today, 15)  # add 15 seconds
newdate2 = DateUtility.addSeconds(today, -20)  # subtract 20 seconds
####
# today:    2023-04-17 14:36:52
# newdate1: 2023-04-17 14:37:07
# newdate2: 2023-04-17 14:26:32

Get time difference (in minutes) between date1 and date2 in hours/minutes

from psdi.app.common import DateUtility

timeDiff = DateUtility.timeDiff(date1, date2)
####
# date1:    2023-04-17 14:46:52
# date2:    2023-04-17 13:22:07
# timeDiff: 84

Make a new Date from the date part of date1 and time part of date2

from psdi.app.common import DateUtility

newDate = DateUtility.combineDate(date1, date2)
####
# date1:   2023-04-17 14:46:52
# date2:   2021-11-23 08:22:07
# newDate: 2023-04-17 08:22:07

Compare date

from psdi.app.common import DateUtility

# Returns True if clickdate is after start date
flag = DateUtility.after(clickdate, start)

# Returns True if the clickdate is before the end date
flag = DateUtility.before(clickdate, end)

# Returns True if clickdate is equals or after start date
flag = DateUtility.afterOrEquals(clickdate, start)

# Returns True if the clickdate is equals or before the end date
flag = DateUtility.beforeOrEquals(clickdate, end)

Compare time

See if date1 is before date2 in hours/minutes only

from psdi.app.common import DateUtility

# Returns -1 if the time of date1 is earlier than the time of date2
# Returns 1 if the time of date1 is later than the time of date2
# Returns 0 if the time of date1 is equal to the time of date2
compareTime = DateUtility.compareTime(date1, date2)

If you found my post interesting or useful and just want to say thanks, you can always buy me a coffee.

Leave a Reply