Select to view content in your preferred language

calculate date field from string field

3924
2
Jump to solution
02-26-2014 10:37 AM
AndrewL
Frequent Contributor
Hi I have the following python code where the field "datetime" is a string field and is different for each record. I want to convert the UTC time string to a date/time and then subtract 6 hours from it (CST).

It is telling me that "!datetime!" does not match the format of "%Y%m%d%H%M". The actual records within the field do match that format, but I think it is trying to use the string "!datetime!" rather than using the data within that field name.

I wonder if this is a quick formatting fix or I need to set up a cursor to go through each field? Thank you.


arcpy.CalculateField_management(pt, "DATE_CST", dt.strptime("!datetime!", "%Y%m%d%H%M")  - timedelta(hours=6), "PYTHON_9.3")
0 Kudos
1 Solution

Accepted Solutions
AndrewL
Frequent Contributor
Thank you. I found some info on the ArcGIS help pages. Here is what I did.

arcpy.ConvertTimeField_management(pt,"datetime","yyyyMMddHHmmss, yyyy/MM/dd HH:mm:ss", "DATETIME_Converted") arcpy.AddField_management(pt, "DATETIME_CST", "DATE") arcpy.CalculateField_management(pt, "DATETIME_CST", "DateAdd(\"h\",-6,[DATETIME_Converted])", "VB", "")

View solution in original post

0 Kudos
2 Replies
RichardFairhurst
MVP Honored Contributor
Hi I have the following python code where the field "datetime" is a string field and is different for each record. I want to convert the UTC time string to a date/time and then subtract 6 hours from it (CST).

It is telling me that "!datetime!" does not match the format of "%Y%m%d%H%M". The actual records within the field do match that format, but I think it is trying to use the string "!datetime!" rather than using the data within that field name.

I wonder if this is a quick formatting fix or I need to set up a cursor to go through each field? Thank you.


arcpy.CalculateField_management(pt, "DATE_CST", dt.strptime("!datetime!", "%Y%m%d%H%M")  - timedelta(hours=6), "PYTHON_9.3")


Build it in Modelbuilder and export a working calculation to a Python script to see the correct way to quote your expression.  The whole expression needs to be quoted.  No calculation or conversion of any dates should be done by your python script.  The only date conversion is done directly by the Calculate Field tool after it passes a string expression to its own internal Python interpreter which does the actual parsing of the expression that leads to the conversion.  You are trying to make Python interpret the expression before passing it to the tool.  As far as your Python script is concerned it should just see this entire expression as a string and nothing more.
0 Kudos
AndrewL
Frequent Contributor
Thank you. I found some info on the ArcGIS help pages. Here is what I did.

arcpy.ConvertTimeField_management(pt,"datetime","yyyyMMddHHmmss, yyyy/MM/dd HH:mm:ss", "DATETIME_Converted") arcpy.AddField_management(pt, "DATETIME_CST", "DATE") arcpy.CalculateField_management(pt, "DATETIME_CST", "DateAdd(\"h\",-6,[DATETIME_Converted])", "VB", "")
0 Kudos