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!
Tess
**edited date format from MM/DD/YYYY hh:mm:ss.s to MM/DD/YYYY hh:mm:ss AM/PM
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
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 afloat
similar to that returned bytime.time()
.
>>> import datetime
>>> now = datetime.datetime.now()
>>> now.timestamp()
1535569540.002099
>>>