Skip to content

Field Calculator (Python): Dates

anthonyblackham edited this page Mar 1, 2018 · 1 revision

Dates can be problematic as discussed in Converting Serial Dates, but the field calculator in arcmap integrates some python libraries to help.

Unix Epoch Time

Unix Epoch Time is defined as the seconds since 1/1/1970 00:00:00 UTC minus the leap seconds since then. The time before 1970 is negative, the time since is positive. If you pay attention to to your bits and bytes you'll realise that using a unix signed 32 bit integer will break things in 2038: 2,147,483,647 seconds after 1/1/1970 the number will switch negative which will cause problems. Good thing 64 bit integers are a thing. Unix Epoch time while not very human readable is utilised in Unix based operating systems, web development, servers, etc.

If for some reason you need to convert from a human readable date to Unix Epoch Time the following is an example of how to do it through the field calculator in ArcMap with the python parser.

Lets first summarise the logic behind this approach.

We remember that the Unix time is defined in seconds since 1/1/1970, these are the steps:

  • Identify whether the date is before or after 1/1/1970
  • Find the difference between the listed date and 1/1/1970
  • Convert the difference to seconds
((arcpy.time.ParseDateTimeString( !DATE!) - datetime.datetime(1970, 1 ,1 ) ).days) * 86400