Date Field to Numeric Value

08-29-2018 10:18 AM
Occasional Contributor

This is probably super straightforward, but I seem to be finding mixed options. What is an efficient way to convert a date field (MM/DD/YYYY hh:mm:ss AM/PM) to a numeric value (serial, epoch...) in a new numeric field? I am looking to better visualize and understand time distributions for a spatial dataset. I am using ArcMap 10.5. 

Thanks in advance!


**edited date format from MM/DD/YYYY hh:mm:ss.s to MM/DD/YYYY hh:mm:ss AM/PM

0 Kudos
2 Replies
MVP Regular Contributor

One way:

from datetime import datetime as DT

oldDate = '2018-08-29 12:05:35'

epoch = DT(1970, 1, 1)
epoch_time = int((DT.strptime(oldDate, '%Y-%m-%d %H:%M:%S') - epoch).total_seconds())
# use '%Y-%m-%d %H:%M:%S %p' in format if using AM/PM
print epoch_time # UTC

# prints: 1535544335‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
MVP Esteemed Contributor

If you are using Python 3, datetime.timestamp() (see 8.1. datetime — Basic date and time types — Python 3.7.0 documentation 😞

Return POSIX timestamp corresponding to the datetime instance. The return value is a float similar to that returned by time.time().

>>> import datetime
>>> now =
>>> now.timestamp()