Summary

Article ID:360032829892
1 minute readKnowledge base

Currently, there is no way of using relative dates to search through the API (for example: last month or 30 days ago).

Solution

Calculate the date/time using the current date and representing it in an accepted format for the findObjects command.

Examples

Create a script with findObjects in it, using the following filter:

{
propertyName => "start",
operator => "lessOrEqual",
operand1 => computeDaysAgoTimeStamp(30)
}

# where computeDaysAgoTimestamp is a sub:

# ----------------------------------------------------------------------

# computeDaysAgoTimeStamp
# Computes current-time minus $daysAgo in GMT time, for passing into
# a date filter (e.g. startTime <= 3 days ago).
#
# Arguments:
# daysAgo - number of days ago.
# ----------------------------------------------------------------------

sub computeDaysAgoTimeStamp($) {
my ($daysAgo) = @_;

# Get current time.
my $curTime = time();

# Subtract $daysAgo days.
$curTime -= 3600*24*$daysAgo;

# Convert this time to y-m-d format and return it.
my ($sec, $min, $hour, $mday, $mon, $year) = gmtime($curTime);
my $dateStr = sprintf("%4d-%02d-%02dT%02d:%02d:%02d.000Z", 1900+$year,
$mon+1, $mday, $hour, $min, $sec);
return $dateStr;
}

Applies to

Commander v.5.x and CloudBees CD (CloudBees Flow) versions 6.x and up