I have a datetime field (Horario) in my feature class, showing only the time. Although the date doesn't appear, it seem to be stored automatically as 30 Dec 1899, I'm not sure why.

I want to use this field to populate another field (Periodo) , where I'll have different periods of the day: morning (6 am - 12pm), afternoon (12pm - 6pm), evening (6pm - 12am) and night (12am - 6am).

I'm trying to write an if-elif statement on the Field Calculator, but I'm always having an "invalid syntax" error, and I'm guessing that's because of the datetime expression I'm using. Can anyone see what's wrong?

def Reclass(horario):
    if (horario < date '1899-12-30 12:00:00' and horario >= date '1899-12-30 06:00:00'):
        return "Manhã (6-12h)"
    elif (horario < date '1899-12-30 18:00:00' and horario >= date '1899-12-30 12:00:00'):
        return "Tarde (12-18h)"
    elif (horario <= date '1899-12-30 23:59:00' and horario >= date '1899-12-30 18:00:00'):
        return "Noite (18-24h)"
    elif (horario < date '1899-12-30 06:00:00' and horario >= date '1899-12-30 00:00:00'):
        return "Madrugada (0-6h)"


I'm thinking the problem is I'm referring to the date as

date '1899-12-30 06:00:00'

How should I refer to datetime fields when using Python?

that's how my table looks like

  • what is the type of field for horario / actual values in your attributes?
    – GISHuman
    Commented Jul 18, 2014 at 13:05
  • The type is date, the actual values are shown like 09:10:00, for instance, but when I do a SQL Query, it appears as date '1899-12-30 09:10:00'. I think that's why I'm getting confuse, I don't know how the actual values are being stored. Commented Jul 18, 2014 at 13:08
  • 1
    Well, AND is not a valid python keyword, so you'll always get an invalid syntax error.
    – Paul
    Commented Jul 18, 2014 at 13:12
  • Omg, I can't believe I missed that! Thanks so much Paul! Unfortunately, that was not the only problem, I'm still getting a sintax error... Commented Jul 18, 2014 at 13:17

1 Answer 1


Using cursors is a good way to inspect how datetime fields can be accessed and written to. This is taken from a feature class, with a a date field:

with arcpy.da.SearchCursor("poly_small", ("COL_DATE")) as cursor:
     for row in cursor:
        print row[0], type(row[0])

2014-07-18 06:03:05 <type 'datetime.datetime'>
2014-07-17 00:00:00 <type 'datetime.datetime'>

So, the easiest way to build this would be with the datetime module.

from datetime import datetime as dt    
test = dt(2014, 7, 18, 6, 3, 5)

with arcpy.da.SearchCursor("poly_small", ("COL_DATE")) as cursor:
     for row in cursor:
        print row[0], type(row[0]), row[0] == test

2014-07-18 06:03:05 <type 'datetime.datetime'> True
2014-07-17 00:00:00 <type 'datetime.datetime'> False

Instead of using and, there is a simpler way to check if a number lies between two numbers see here I believe the following is correct:

def Reclass(horario):

    if dt(1899,12,30, 12,0,0) > horario  >= dt(1899,12,30, 6,0,0):
        return "Manhã (6-12h)"

    elif dt(1899,12,30, 18,0,0) > horario >= dt(1899,12,30, 12,0,0):
        return "Tarde (12-18h)"

    elif dt(1899,12,30, 23,59,0) >= horario >= dt(1899,12,30, 18,0,0):
        return "Noite (18-24h)"

    elif dt(1899,12,30, 6,0,0) > horario >= dt(1899,12,30, 0,0,0):
        return "Madrugada (0-6h)"
  • That makes a lot of sense, Paul! Thanks for the throughout answer! It's really helping me understand how to use date with python. I used the code as shown, but I get the error File "<expression>", line 1, in <module> File "<string>", line 4, in Reclass TypeError: can't compare datetime.datetime to unicode It's weird, because the only string I have it's after return. Any guess? Commented Jul 18, 2014 at 14:30
  • @Beatriz Viseu, horario is in unicode, likely.
    – Paul
    Commented Jul 18, 2014 at 14:34
  • Yeah, so I changed my code to the '1899-12-30 06:00:00' style, and it's now comparable. It even worked with a simpler code (if horario > '1899-12-30 06:00:00': return "Manhã"). However, it doesn't seem to like when I use multiple statements (horario>x and horario<y / x>horario>y). I tried both ways, the tool does run, I don't get any error, but all my records keep as <Null>. I have no idea what's wrong. Commented Jul 21, 2014 at 16:16
  • @BeatrizViseu, So you aren't using the datetime module at all? You might want to update your question with the new code or start another one if you have a different question entirely.
    – Paul
    Commented Jul 21, 2014 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.