I’m not sure if this is a bug or simply a feature implemented specifically like this, but I’m trying to set a default date for the date type, using the same mechanism as for other types.
so in the calculation procedure, i add something like:
import datetime mydate = datetime.date(2020, 4, 1)
It doesn’t matter what I put as a date or datetime object, the result is always the same : the actual displayed date in the test itself is 20 Dec 2017. I’ve tried with
META['work_started'], and a bunch of other. It’s always 20 Dec 2017. (Why this particular date ??)
Then I tried to get the result of this date field in another composite, and it looks like I received “2017-12-20”. So I tried to provide a string using the same format, like “2020-04-01”, but again only “20 Dec 2017” was displayed.
Last thing I tried was to provide a string using the same displayed format. I tried with “01 Apr 2020”, and it actually worked. The displayed date was indeed 01 Apr 2020, and when I clicked on the calendar, it was set to 01 Apr 2020.
So it looks like the date object is not exactly a true python date object or I’m missing something maybe.
I just tried one last thing : first create an actual date object with
d = datetime.date(2020, 4, 2) then set mydate to
d.strftime('%d %b %Y')
So you really need to put a string in this particular format. I assume that there are actually two objects behind this date type : the actual python date object, and its string representation that gets displayed in the box.
There are ways to improve this, imho:
- allow the users to change the date format to whatever they want, much like they can with numerical types. As long as you use the correct python syntax like ‘%Y-%m-%d’, the displayed text should use the strftime method from datetime objects to display it in the box. This is also helpful for international situations where you don’t want to see “Apr” for April. Or for dicom consistency where every date is in the format %Y%m%d like 20200401
- when you set a default value, set an actual date or datetime python object rather than a string
- I also wonder whether it’s really useful to have date and datetime. Whatever you do with a date object, you can also do with a datetime object. The reverse is not true.