Timeline for Using arcpy and script toolbox to add the date to a attribute table
Current License: CC BY-SA 3.0
19 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Dec 7, 2014 at 17:30 | comment | added | Cam | Amazing! Thank you for your help, your revised version works, and I do see the usefulness of keeping it in python format, thanks again! | |
Dec 7, 2014 at 17:00 | history | edited | Richard Fairhurst | CC BY-SA 3.0 |
added 100 characters in body
|
Dec 7, 2014 at 16:54 | comment | added | Richard Fairhurst | Use the revised code to get the date and time using the Python_9.3 calculation. The strptime function correctly calculates the entire date and time in my script now that I have converted it to a string expression for the calculator, rather than doing the conversion in the script directly. VB Script should not be used if you want the time preserved, since it will strip it out, and the Python version will make the follow up calculation easier for the difference. | |
Dec 7, 2014 at 16:49 | history | edited | Richard Fairhurst | CC BY-SA 3.0 |
added 92 characters in body
|
Dec 7, 2014 at 16:45 | comment | added | Richard Fairhurst | The calculation should now work if you use my latest revised code. I still recommend the strptime function expression over the VB Script #date# expression, since the strptime can be adjusted to handle many different date formats, while the VB Script #date# function does not let you specify the date format it accepts. | |
Dec 7, 2014 at 16:42 | history | edited | Richard Fairhurst | CC BY-SA 3.0 |
added 551 characters in body
|
Dec 7, 2014 at 16:27 | comment | added | Cam | Using the date1 = '#' + arcpy.GetParameterAsText(1) + '#' solved the problem. It adds the date to the table now :) It doesn't add the time for h:m:s to the table tho, which is fine as long as I can calculate the difference in time between them. | |
Dec 7, 2014 at 16:22 | vote | accept | Cam | ||
Dec 7, 2014 at 16:08 | comment | added | Richard Fairhurst | The hashtage #date# format applies to VB Script calculations, which is what you are using by not specifying the calculation type (the parameter after the date expression). For Python calculations I believe you would have to set the expression up to use the strptime function to get the field calculator to treat the string as a date, but I believe that function expression needs to be a string expression to work in a script so that the field calculator does the date conversion, not the script. It trips me up often, since it works one way in Desktop and another in a script. | |
Dec 7, 2014 at 15:57 | comment | added | Richard Fairhurst | You should update your code to show what you are now trying, since I really cannot tell what line you are referring to, since you are not repeating my code to explain the problem. I have no idea what "THEDATE" represents. Is it a real python date, a string, ??? | |
Dec 7, 2014 at 15:55 | comment | added | Richard Fairhurst | You cannot use a date string in the calculation without enclosing it in the format "#date#" The hashtags enclosing the string tell the field calculator to use it as a date. So for the field calculation you may not want a real date and just use: date1 = '#' + arcpy.GetParameterAsText(1) + '#'. You also could use an updatecursor to accomplish same thing as a field calculation and the cursor would correctly accept the real python date. The real python dates could also do the date difference operation without every storing the actual dates in a table. | |
Dec 7, 2014 at 15:15 | comment | added | Cam | I believe my add field management is incorrect, it seems that it does not want to add a date format correctly, rather it wants to add the year from amount of days starting at 1900. The error arises from inputting 2014-12-01 22:37:28, says that its in string format and error 999999 comes up, using just python with arcpy.CalculateField_management("Mother1", "Date", "THEDATE") and lets says THEDATE is "2014-12-01 22:37:28" I get the 99999 error, however if THEDATE is something like 3650 then I get it to work, but the date is 1909-12-28 which counts from 1900... I'm getting it slowly :) | |
Dec 7, 2014 at 9:25 | comment | added | Richard Fairhurst | If you printing date1 after the conversion then that makes no difference (it is a valid date if the conversion succeeded). It only makes a difference what print arcpy.GetParameterAsText(1) prints like. Are you saying that the print arcpy.GetParameterAsText(1) strings is 2014-12-01 22:37:28? If so that isn't a problem. I just have to match that format, so I changed the strptime format to: date1 = datetime.strptime(arcpy.GetParameterAsText(1), '%Y-%m-%d %H:%M:%S') | |
Dec 7, 2014 at 9:21 | history | edited | Richard Fairhurst | CC BY-SA 3.0 |
deleted 8 characters in body
|
Dec 7, 2014 at 4:12 | comment | added | Cam | :) Almost! I will use the getparameterastext format. The format in which I'm printing the code is 2014-12-01 10:37:28 PM. However when I check what date1 = datetime.strptime(arcpy.GetParameterAsText(1), '%Y-%m-%d %I:%M:%S %p') is by print date1. I get 2014-12-01 22:37:28. | |
Dec 7, 2014 at 3:58 | history | edited | Richard Fairhurst | CC BY-SA 3.0 |
I moved the try block to have any date conversion error captured by the exception.
|
Dec 7, 2014 at 3:48 | history | edited | Richard Fairhurst | CC BY-SA 3.0 |
I moved the try block to have any date conversion error captured by the exception.
|
Dec 7, 2014 at 3:28 | comment | added | PolyGeo♦ |
+1 I was just starting to look at this and my first comment on the question was going to be that I would use arcpy.GetParameterAsText() rather than sys.argv[] which you have covered. However, the focus of this question should always have been date/string formatting and you have used a technique that I am sure I will be looking back at next time I need to handle dates in ArcPy.
|
|
Dec 7, 2014 at 3:23 | history | answered | Richard Fairhurst | CC BY-SA 3.0 |