datetime.combine (datetime.strptime ( !DateTime!, %Y %m %d), datetime.strptime (!Time!, %H:%M:%S))
Solved! Go to Solution.
the Field Caclulator and by extension, the Calculate Field geoprocessing tool access the date and time values as strings. In a file geodatabase these are all unicode strings, though you may find they're not in the case of a personal geodatabase.
We can use datetime.strptime to convert the two strings of text into datetime objects. Then we can access the properties of each of these objects to pull out information about the day / month / year in the case of our field with only the date, in it, or hour / minutes / seconds in the case of our field containing both a date and time. When the Calculate Field goes to fill in the time information in the combined field, it wants this data to be provided as a datetime object though. So this makes our best bet, to take the information we're interested from each of our two existing datetime objects, and create a new datetime object using datetime.datetime(yeah, month, day, hour, minute, second).
Expression: combine_datefields(!Time_!, !DateTime_!) Codeblock: def combine_datefields(time_value, date_value): t = datetime.datetime.strptime( time_value.encode('utf-8'), "%d/%m/%Y %I:%M:%S %p") d = datetime.datetime.strptime( date_value.encode('utf-8'), "%d/%m/%Y") combined_dt = datetime.datetime(d.year, d.month, d.day, t.hour, t.minute, t.second) return combined_dt
... are the !DateTime! and !Time! fields strings, or are they actual date fields in the table?
If the dates are date objects with 00:00:00 time components,
You might be able to add the time field value to the dateTime field value
>>> import datetime >>> d = datetime.datetime.now() >>> d datetime.datetime(2014, 1, 28, 15, 28, 21, 470000) >>> tup = d.timetuple() >>> tup time.struct_time(tm_year=2014, tm_mon=1, tm_mday=28, tm_hour=15, tm_min=28, tm_sec=21, tm_wday=1, tm_yday=28, tm_isdst=-1) >>> for t in tup: print t 2014 1 28 15 28 21 1 28 -1
### dateAndHour.py import datetime hrObj = datetime.datetime(1999, 02, 06, 05, 40, 29) dtObj = datetime.datetime(2014, 1, 29, 00, 00, 00) t = hrObj.timetuple() d = dtObj.timetuple() newDate = datetime.datetime( d[0], d[1], d[2], t[3], t[4], t[5]) print hrObj print dtObj print newDate
the Field Caclulator and by extension, the Calculate Field geoprocessing tool access the date and time values as strings. In a file geodatabase these are all unicode strings, though you may find they're not in the case of a personal geodatabase.
We can use datetime.strptime to convert the two strings of text into datetime objects. Then we can access the properties of each of these objects to pull out information about the day / month / year in the case of our field with only the date, in it, or hour / minutes / seconds in the case of our field containing both a date and time. When the Calculate Field goes to fill in the time information in the combined field, it wants this data to be provided as a datetime object though. So this makes our best bet, to take the information we're interested from each of our two existing datetime objects, and create a new datetime object using datetime.datetime(yeah, month, day, hour, minute, second).
Expression: combine_datefields(!Time_!, !DateTime_!) Codeblock: def combine_datefields(time_value, date_value): t = datetime.datetime.strptime( time_value.encode('utf-8'), "%d/%m/%Y %I:%M:%S %p") d = datetime.datetime.strptime( date_value.encode('utf-8'), "%d/%m/%Y") combined_dt = datetime.datetime(d.year, d.month, d.day, t.hour, t.minute, t.second) return combined_dt