Hiding Date in CRM DateTime Attributes

Roshan Mehta, 03 March 2010

When creating a new DateTime attribute in Microsoft Dynamics CRM 4.0, the format drop-down-list only allows the customizer to select one of two values: Date Only or Date and Time. But what if you want to create a new attribute to show the time only? I came across this scenario when wanting to add information about Account business hours, and found a very useful solution using Javascript on the entity form’s onLoad event handler.

Firstly, you need to select “Date and Time” for the new Time attribute’s format. Next, you need to use the following Javascript code within the onLoad event handler.

document.getElementById("mag_time").childNodes[0].childNodes[0].style.display = "none";

This hides the text input area of the DateTime field, but users can still select a date using the calendar control. We want to eliminate this, so you need to include the following:

document.getElementById("mag_time").childNodes[0].childNodes[1].style.display = "none";

Adding this line also hides the calendar control. We are now left with exactly what we want, the time area of the DateTime field on its own. However, the field is completely disabled. To get around this, all we need to do is make sure we set a default value for the time field within the entity’s onLoad event handler. This should appear before the code snippets listed above.

var date = new Date(); date.setHours(8, 30, 0, 0); crmForm.all.mag_time.DataValue = date; And there we have it, a custom time field for you to use on any entity.