I have written a script and have an arcpy tool in a custom toolbox to fill in the parameters.
Two of my parameters are dates.
The Date data-type format for the parameters has a default setting of date and time format to the script.
I'd like to only pass the date onto my script. I included some screenshots of my parameters and the date data-type in the tool.
This old geonet question asks the same sort of thing for a python toolbox (.pyt) but doesn't provide an answer.
Has anyone found a way to access the properties of the date data type? I haven't been able to find any help in the documentation.
I'm working in ArcGIS 10.4.
This is my last-ditch effort before I strip it apart myself in the validation.
Thanks,
Natasha
can't find anything so I presume that you are going to have to do the split thing
>>> from datetime import datetime
>>> str(datetime.now())
'2017-01-26 17:21:42.613751'
>>> str(datetime.now()).split(" ")[0]
'2017-01-26'
Yes, I ended up splitting the date/time components like you mentioned Dan.
I found some interesting things with the Date data type:
I found those funny things with the date/time because I wanted to send an error if a Time type was entered. The validation always returned true when looking for time, even when there was only a date in my parameter.
Below is how I went about striping out the time and keeping only the date. I am using this tool with different date formats which is why you see a reference to a dictionary. And the second chunk of code shows how I use this function in the Update Messages potion of the validation.
def checkIfTimeOnly(self, winDate, theDate):
dateEdit = str(theDate)
dateSplit = dateEdit.split()
dateDate = dateSplit[0]
zeroDate = datetime.strptime("1899-12-30", "%Y-%m-%d").date()
dateForm = datetime.strptime(str(dateDate), date_dictionary[winDate]).date()
#if date is the zero date then we have a time type, not date or date/time type
if dateForm == zeroDate:
return True
else:
return False
if self.params[7].altered:
if self.params[7].value:
isTime = self.checkIfTimeOnly(winDateValue, self.params[7].value)
if isTime:
self.params[7].setErrorMessage("Is not a date")
if self.params[8].value == None:
self.params[8].value = self.params[7].value
if self.params[8].altered:
if self.params[8].value:
isTime = self.checkIfTimeOnly(winDateValue, self.params[8].value)
if isTime:
self.params[8].setErrorMessage("Is not a date")
#Check param8 or End Date is not before the start/select date.
if self.params[7].value and self.params[8].value:
if self.params[7].value > self.params[8].value:
self.params[8].setErrorMessage("End date must be later than Start Date.")
Cheers,
Natasha
good to note... I hate dates anyway, so I just usually convert to string and work from that, but I guess when you don't have the luxury to fix existing tables, this will come in handy
Unfortunately the Esri documentation on Python toolboxes is indeed very poor.
With trial and error, I found out that the field must be initialized with a date value. If the value is set as date in the getParameterInfo function, then Date is selected in the date-time picker.
For example (param3 is datatype GPDate):
param3.value = "1/1/2020"
The anonymous post is the correct answer: "With trial and error, I found out that the field must be initialized with a date value. If the value is set as date in the getParameterInfo function, then Date is selected in the date-time picker."
Here is the code I used to set the current date as default:
import datetime
p0 = arcpy.Parameter("startDate","Date Start:","INPUT","GPDate","REQUIRED")
p0.value = str(datetime.datetime.now().date())
Similar question by me: running a tool which does some "datetimes" calculations needs to convert the string value into a datetime object. Can I use system locale formatting to get correct datetime values? (I need to share my toolbox with users that run windows under different languages.)
I did something like that:
from datetime import datetime
p0 = arcpy.GetParameterAsText(0) # A date/time string
if len(p0.split(' ')) == 1:
_format = '%x'
else:
_format = '%x %X'
dt = datetime.strptime(p0, _format)
Thanx!
After all, to use strptime with different locales, I now use win32api to find the id for the current user locale and set locale to it. The format string "%x %X" than accepts my parameter. Later in my script I'm able to get the date/time components using _start_dt.year, _start_dt.month, _start_dt.day and so on.
import arcpy
from datetime import datetime
import locale
import win32api
# parameter value (of type date)
start_time = arcpy.GetParameterAsText(0)
# internal ID for user locale for windows
_lcid = win32api.GetUserDefaultLCID()
_loc = locale.windows_locale[_lcid]
locale.setlocale(locale.LC_ALL, _loc)
_format = '%x %X'
# now create datetime using strptime with locale formatting
_start_dt = datetime.strptime(start_time, _format)
Maybe someone finds that useful. Thank you!