Custom Date Format for the Work Item DateTimeControl (TFS2010)


While working on a new work item template (WIT) for Team Foundation Server 2010 I encountered a requirement for a field where the user could select the time a call was received. The DateTimeControl for TFS work items displays only a date picker by default, which for this particular use case was not quite accurate enough.

My first reaction was to start building a custom work item control – but with a bit of digging into the existing one using Reflector I discovered that the ability to change the control format was indeed supported, just not immediately obvious.

It turns out that for any implementation of IWorkItemControl, the Properties property is set to contain all the attributes on the controls declaration line within the WIT. For example, the following control declaration will result in four entries within the Properties collection (FieldName, Type, Label and LabelPosition).

<Control
FieldName=Support.TimeReceived
Type=DateTimeControl
Label=Time Received:
LabelPosition=Left />

Additional attributes can be added to the control without invalidating the schema, so work item controls can make use of this feature to support additional configuration parameters. The DateTimeControl includes the following properties that can be set using this mechanism:

  • Format: A valid selection from the DateTimePickerFormat enumeration (Long, Short, Time, Custom)
  • CustomFormat: If the Format is set to Custom then the CustomFormat property is used to define the display format. This uses the standard formatting strings for .NET date formats.

Thus the following control definition can be used to add a Time-aware picker to a work item.

<Control
FieldName=Support.TimeReceived
Type=DateTimeControl
Label=Time Received:
LabelPosition=Left
Format=Custom
CustomFormat=dd/MM/yyyy hh:mm:ss
/>

For anyone that prefers to use the WIT editor within the TFS Power Tools (found here), these properties can be added using the String Collection Editor associated with the Attributes property for the DateTimeControl.

As an added bonus, the control is rendered in correctly in both Team Explorer and Team Web Access views of the work item. Unfortunately it is not possible to change the DateTime selection to use the spin control (aka the up-down control) rather than the calendar drop down, but that is a relatively minor problem.

15 comments

  1. Great posting. Thanks.

    When a work item is created, we would like to validate the date during state transition. Specifically, we do not want dates < today. Can this be done?

    Thanks!

  2. This is a very useful post! Thanks for that. However, the sample date customformat won’t give AM or PM on the end of the date, so use this one instead:
    dd/MM/yyyy hh:mm:ss tt

    • Hi Dylan,

      Fantastic, thanks for the tweaked format. I’ve had the same problem for a while now, but hadn’t taken the time to investigate the format.

      Cheers,

      Nick

  3. Thanks so much for this! I am using this format in TFS 2010:
    CustomFormat=”dddd dd/MMM/yyyy hh:mm:ss tt” however on the TFS web access the work item is showing up with the date and time as: “8on15ay 15/Aug/2011 04:24:24 pm”. Strange that the same date format would show up differently on the web and within TFS for the same WIT. Both lines in the XML are the same.

    This is the line –

    Type=”DateTimeControl” Label=”Installation &Date/Time:” LabelPosition=”Left” Format=”Custom” CustomFormat=”dddd dd/MMM/yyyy hh:mm:ss tt”

  4. (Posting again because I forgot to select to notify me of comments)

    Thanks so much for this! I am using this format in TFS 2010:
    CustomFormat=”dddd dd/MMM/yyyy hh:mm:ss tt” however on the TFS web access the work item is showing up with the date and time as: “8on15ay 15/Aug/2011 04:24:24 pm”. Strange that the same date format would show up differently on the web and within TFS for the same WIT. Both lines in the XML are the same.

    This is the line –

    Type=”DateTimeControl” Label=”Installation &Date/Time:” LabelPosition=”Left” Format=”Custom” CustomFormat=”dddd dd/MMM/yyyy hh:mm:ss tt”

  5. Alyson, I see you posted to the msdn forums on this. Interesting. For some reason, the date format is being reapplied, such that the M in monday is being turned into the number 8 (for the “M”onth) and the d into the number 15 (for the “d”ay.) It is as if the output of the format is then being reapplied as the format for a secondary output.

    Curious, does Friday that week render as Fri19ay?

    When Microsoft tried to reproduce the issue, they tried on a Friday.

  6. This seems like exactly what I’m looking for, however it’s not working. I’m using TFS Power Tools(VS 2010) to edit a Work Item. Here’s my XML for the control in question:

    However when I preview the form, all I’m getting is the standard Date drop down box. Are there any other requirements to make this work that I might be missing? Thanks in advance for any help.

  7. I just realized that my XML was formatted out. Here’s the code without the tags(not sure how to include them:

    Control FieldName=”EagleSeven.ReportedDate” Type=”DateTimeControl” Label=”Reported Date:” LabelPosition=”Left” Format=”Custom” CustomFormat=”M/d/yy h:mm:yy tt” /

    • Hi Prasath,

      I’m not sure that I understand the question. We use dates in the future in a few work items (such as expected SLA First Response times etc), and haven’t had any problem.

      Regards,

      Nick

  8. Hello, I need to remove time field from datetime field. Can any one help to remove time after selecting date from calender.

Leave a reply to Alyson Cancel reply