You have mixed SQL syntax with Python syntax. The Select by Attributes uses SQL but the Field Calculator python parser uses python.
def getHalf(dfield):
df = datetime.datetime.strptime(dfield, '%Y/%m/%d %H:%M:%S')
d1 = datetime.datetime.strptime('2017-08-27 00:00:00', '%Y/%m/%d %H:%M:%S')
d2 = datetime.datetime.strptime('2017-08-27 12:00:00', '%Y/%m/%d %H:%M:%S')
if df >= d1 and df < d2:
return 1
This may depend on how your dates are stored - mine are different to yours, so I've had to use the strptime()
format differently to yours, and I have tried to adjust here based on the dates you've shown in your question. For more info on the formatting tags see Python - Basic date and time types - 8.1.7. strftime() and strptime() Behavior
Explanation:
df = datetime.datetime.strptime(dfield, '%Y/%m/%d %H:%M:%S')
Tell python that your date field timestamp_cst
contains a date. Python now stores this as a datetime object df
.
d1 = datetime.datetime.strptime('2017-08-27 00:00:00', '%Y/%m/%d %H:%M:%S')
d2 = datetime.datetime.strptime('2017-08-27 12:00:00', '%Y/%m/%d %H:%M:%S')
Put your start/end dates (times) into datetime objects for comparison against your date field.
if df >= d1 and df < d2:
return 1
This runs the comparison of the datetime objects, so you are comparing datetime against datetime against datetime, not string against string or string against date etc.
datetime
to ensure you're treating them as dates