Comparing parts of a date and time field

Use the static DatePart method of the DBFunction class to extract a portion of a java.util.Date field to use in a comparison predicate. The DatePart method takes two parameters. The first parameter specifies the part of the date and time you want to extract, and the second parameter specifies the field from which to extract the part. The DatePart method returns the extracted part as a java.lang.Number.

DatePart(datePart : DatePart, date : ColumnRef) : DBFunction

For the datePart parameter, you can specify HOUR, MINUTE, SECOND, DAY_OF_WEEK, DAY_OF_MONTH, MONTH, or YEAR. When you specify DAY_OF_WEEK as the part to extract, the first day of the week is Monday.

The following Gosu codes uses the DatePart method to extract the day of the month from the due date on activities. The query builder codes uses the returned numeric value in a comparison predicate to select activities that are due on the 15th of any month.

uses gw.api.database.Query
uses gw.api.database.DBFunction
uses gw.api.database.QuerySelectColumns
uses gw.api.path.Paths

var query = Query.make(Activity) 
// Query for activities with due dates that fall on the 15th of any month.
query.compare(DBFunction.DatePart(DAY_OF_MONTH, query.getColumnRef("endDate")), Equals, 15)

// Order the result by assignment date and iterate the items fetched. 
for (activity in (query.select().orderBy(
        QuerySelectColumns.path(Paths.make(Activity#AssignmentDate))))) {
  print("Assigned on " + activity.AssignmentDate + ": " + activity.DisplayName) 
}