I have developed a Python script using ArcPy with one of the output variable being of Date type. In the actual Python code, the variable is a Python datetime object which has both the date and time sections.
A snippet of Python script "ExtractNetCDFToRaster.py" is as below.
import sys, osfrom os.path import basenameimport arcpyimport datetimefrom dateutil import tz # Convert UTC datetime to local datetimedef convertToLocalDT(string): from_zone = tz.tzutc() to_zone = tz.tzlocal() # 20/11/2017 1:00:00 PM dtUTC = datetime.datetime.strptime(string, "%d/%m/%Y %I:%M:%S %p") dtLocal = dtUTC.replace(tzinfo=from_zone).astimezone(to_zone) return dtLocal ...# get dimension values from the time dimensiondimension_values = nc_FP.getDimensionValue("time", 0)# the dimension_values is of a unicode type of data, e.g. u'20/11/2017 1:00:00 PM'strDimensionValues = dimension_values.encode('utf-8')arcpy.AddMessage("dimension_values: " + strDimensionValues)dtDimensionValues = convertToLocalDT(strDimensionValues)...# Set dtDimensionValues as output which has an alias of "outDateTime" in the tool Properties window.arcpy.SetParameter(5, dtDimensionValues)
When I imported this script into a Model as a custom tool and ran the tool, the variable only output the date but not the time section. For example, if the actual datetime is "2017-11-21 00:00:00" the outDateTime only shows "21/11/2017" as below.
Solved! Go to Solution.
Yes... your alternative would be as I suggested which would complicate your workflow, since you would have to do the date conversion to string, then add the result to a text field and not a date field. I would suggest the FC in the gdb
Would an output string be what you are looking for from the python side?
n = datetime.datetime.now()
n
datetime.datetime(2017, 11, 20, 1, 14, 19, 245760)
n.__str__()
'2017-11-20 01:14:19.245760'
@Dan. Thanks! I can do String output from the script. But the script will be used as a custom tool to be imported into a main Model called "MainModel". The MainModel will add the output date and time to a field of Date type (called "_Time") in a Shapefile. Does the "_Time" field automatically pick up a String as Date type?
If it is adding it to shapefile..,. time is not supported wish I had known that initially....
date fields only support date. They do not support time.
The nonsupport of time in date fields can be a serious limitation for any tool that performs temporal analysis, such as those found in the Space Time Pattern Mining toolbox. Avoid using shapefiles for any kind of temporal analysis or date time calculation.
Thanks Dan. You are right. The help documentation states:
Fundamentals of date fields—Help | ArcGIS for Desktop
Displaying dates
A coverage or shapefile stores dates in a date field with this format: yyyy-mm-dd. A geodatabase formats the date as datetime yyyy-mm-dd hh:mm:ss AM or PM. Settings on your Windows system determine how the dates are displayed in ArcMap—M/d/yy, MM/dd/yy, yy/MM/dd, and so on. ArcMap uses the system short date format (numerical) for displaying dates.
So I should use feature class in a file geodatabase instead of shapefile.
Yes... your alternative would be as I suggested which would complicate your workflow, since you would have to do the date conversion to string, then add the result to a text field and not a date field. I would suggest the FC in the gdb