Skip to main content

HSD’s Senior Technical Consultant, Kamran Dilmir discusses one of the client API features in the latest release of Dynamics 365 CE.

In the latest release of Dynamics 365, Microsoft has introduced many new features in Client API. In this blog post, I will explain one of the client API features to show/hide the time part of the Date time control.  

The functionality to configure the format of a DateAndTime field has always been there. However, if we consider a requirement where there is a date time field and we have to display it in two forms, one with time and one without the time (date only), it was not possible before. Previously we could have achieved this functionality by creating two fields. One with date and time format option added to the first form, and another with date only format and added to the second form. 

With the latest release, we can optimise the above solution by achieving the functionality using the same field. The same date time field can be used on both forms and we can dynamically show/hide the time portion using the client API’s getShowTime() and setShowTime() functions. 

For the below example we will use a DateAndTime field “new_deliverydate” with the format set to date and time. Now first we have to check whether the time part of the date time field is shown or not. To check this we can use the following function from the client API. 

getShowTime():  

This validates whether the date control has the time portion or not. It returns true if it shows the time portion of the date and false if not shown. The method getShowTime() does not work if the format of the field is set to DateOnly. 

e.g. formContext.getControl(“new_deliverydate”).getShowTime(); 

Now let’s have a look at how to show/hide the time portion of the field. This can be achieved by using the setShowTime() function of the client API. 

setShowTime(): 

This function helps to show/hide the time part of the DateTime field in the form. By passing false in function parameter to hide the time. 

e.g. formContext.getControl(“new_deliverydate”).setShowTime(false); 

 
In summary, we can show/hide the time component of a date control where the attribute uses the DateAndTime format. 

Make sure you check out more of HSD’s blog posts by visiting https://www.hsd.com.au/blog/ and follow us on LinkedIn for up-to-date activity from HSD.