# Name: SelectLayerByDateAndTime.py
# Description: Select crimes that occurred in the past 28 days, from 9 PM to 1 PM, on the weekend, from the month of October to December,
# in the year 2018, and show the selection count. Finally, make a selection based on all the queries above.
# Import script modules
import arcpy
# Set the workspace
arcpy.env.workspace = r"C:\data\city_pd.gdb"
# Convert Feature class to table view
arcpy.management.MakeTableView("Crimes", "Crimes_View")
lyr, count = arcpy.ca.SelectLayerByDateAndTime(in_layer_or_view="Crimes_View",
selection_type="NEW_SELECTION",
time_type="SINGLE_TIME_FIELD",
date_field="offendate",
selection_options="DATE",
date_selection_type="RECENCY",
time_slice="28 Days")
arcpy.AddMessage("Crimes that occurred in past 28 days: {}".format(str(count)))
arcpy.management.SelectLayerByAttribute("Crimes_View","CLEAR_SELECTION")
lyr, count = arcpy.ca.SelectLayerByDateAndTime(in_layer_or_view="Crimes_View",
selection_type="NEW_SELECTION",
time_type="SINGLE_TIME_FIELD",
date_field="offendate",
selection_options="TIME",
start_time="9:00 PM",
end_time="1:00 AM")
arcpy.AddMessage("All crimes that occurred between 9PM and 1AM: {}".format(count))
arcpy.management.SelectLayerByAttribute("Crimes_View","CLEAR_SELECTION")
lyr, count = arcpy.ca.SelectLayerByDateAndTime(in_layer_or_view="Crimes_View",
selection_type="NEW_SELECTION",
time_type="SINGLE_TIME_FIELD",
date_field="offendate",
selection_options="DAY_OF_WEEK",
days_of_week=["SATURDAY", "SUNDAY"])
arcpy.AddMessage("All crimes that occurred on the weekend: {}".format(count))
arcpy.management.SelectLayerByAttribute("Crimes_View","CLEAR_SELECTION")
lyr, count = arcpy.ca.SelectLayerByDateAndTime(in_layer_or_view="Crimes_View",
selection_type="NEW_SELECTION",
time_type="SINGLE_TIME_FIELD",
date_field="offendate",
selection_options="MONTH",
months=["OCTOBER", "NOVEMBER", "DECEMBER"])
arcpy.AddMessage("All crimes that occurred October through December: {}".format(count))
arcpy.management.SelectLayerByAttribute("Crimes_View","CLEAR_SELECTION")
lyr, count = arcpy.ca.SelectLayerByDateAndTime(in_layer_or_view="Crimes_View",
selection_type="NEW_SELECTION",
time_type="SINGLE_TIME_FIELD",
date_field="offendate",
selection_options="YEAR",
years=[2018])
arcpy.AddMessage("All crimes that occurred in the year 2018: {}".format(count))
arcpy.management.SelectLayerByAttribute("Crimes_View","CLEAR_SELECTION")
# Combine selection options for more detailed time queries
lyr, count = arcpy.ca.SelectLayerByDateAndTime(in_layer_or_view="Crimes_View",
selection_type="NEW_SELECTION",
time_type="SINGLE_TIME_FIELD",
date_field="offendate",
selection_options=["TIME", "DAY_OF_WEEK", "MONTH","YEAR"],
start_time="9:00 PM",
end_time="1:00 AM",
days_of_week=["SATURDAY", "SUNDAY"],
months=["OCTOBER", "NOVEMBER", "DECEMBER"],
years=[2018])
message = """All crimes that occurred in the year 2018 during the months
of October through November on the weekends from 9PM to 1AM: {}
""".format(count)
arcpy.AddMessage(message)
arcpy.management.SelectLayerByAttribute("Crimes_View", "CLEAR_SELECTION")