Understand setShowTime With Webresource In Dynamics CRM

Introduction

In Dynamics 365 CRM, at times we must show only the date part for a field of date time type if all the customers aren't in favour of creating the same field with a date only format. This can be achieved by the usage of setShowTime Client API reference, which specifies whether a date control should show the time portion of the date or not. As an example, Vaccination date is used to explain this functionality.

Step 1

Login to the required environment and select required solution [Contact Customizations Solution in this case] as shown in the below figure.

Understand setShowTime With Webresource in Dynamics CRM

Step 2

After Step 1, select contact web resource in solution and click on Edit as shown in the below figure.

Understand setShowTime With Webresource in Dynamics CRM

Step 3

After Step 2, in contact web resource javascript file write the code to show only date part of the date time field using the below syntax

formContext.getControl(arg).setShowTime(bool);
As
formContext.getControl("cr5bc_vaccinationdate").setShowTime(false);

Step 4

After Step 3, write the code to show required date in the following method

function showvaccinationDatePart(executionContext) {
    let formContext = executionContext.getFormContext();
    if (formContext !== null && formContext != 'undefined') {
        formContext.getControl("cr5bc_vaccinationdate").setShowTime(false);
    }
}

Step 5

After Step 4, call this function from on load event of contactform with the below code

handleOnLoad: function(executionContext) {
    console.log('on load - contact form');
    showvaccinationDatePart(executionContext);
}

Step 6

After Step 5, now the complete code looks like this

if (typeof(ContosoVaccination) == "undefined") {
    var ContosoVaccination = {
        __namespace: true
    };
}
if (typeof(ContosoVaccination.Scripts) == "undefined") {
    ContosoVaccination.Scripts = {
        __namespace: true
    };
}
ContosoVaccination.Scripts.ContactForm = {
    handleOnLoad: function(executionContext) {
        console.log('on load - contact form');
        showvaccinationDatePart(executionContext);
    },
    __namespace: true
}

function showvaccinationDatePart(executionContext) {
    let formContext = executionContext.getFormContext();
    if (formContext !== null && formContext != 'undefined') {
        formContext.getControl("cr5bc_vaccinationdate").setShowTime(false);
    }
}

Step 7

After Step 6, save and publish the Webresource and open a contact record and observe vaccination date field was shown with only Date part of a calendar control as shown in the below figure

Understand setShowTime With Webresource in Dynamics CRM

Note

  1. Make sure to publish all customizations and upload JavaScript (js) file.
  2. Vaccination Date custom field was created with date time data type in contact record and then placed on the contact form and saved and published changes.
  3. Contact Form on load event should be registered as an event handler after creation / updation of the new/existing Webresource of type js.
  4. Microsoft documentation found here.

Conclusion

In this way, one can easily use setShowTime Client API Reference to show only date part of the date time field using custom code with web resource in Dynamics 365 CRM.