How to Show or Hide the Time Part of DateTime Column using client API in Model-driven app

By | May 17, 2021

Introduction

As we know in the latest release Microsoft has introduced many new features in the Client API. There are number of client APIs provided by Microsoft that help developers work seamlessly with Dynamics 365 CRM. In this blog, we will explore one of the Client API to Show or Hide the Time part of the Date Time control.

Scenario:

Suppose, there is a datetime type of field/column and two different forms. Now, the user wants to show Date and Time on FORM1, and on FORM 2, the user only wants to show Date. So in this case, we can use the Client API to show/hide the time part of the DateTime field by using getShowTime and setShowTime functions.

We have created a new datetime field named as ‘Date Shipped’ which will display the date and time on the form as shown below. Now, let’s explore both (setShowTime, getShowTime ) functions here.

Show or Hide Time Part of Date Time Column using client Api

1. setShowTime():

This function will help to show/hide the time part of the DateTime field in the FORM. Basically, you need to pass false in function parameter to hide the time.

Example: formContext.getControl(“new_dateshipped”).setShowTime(false);

As you can see in the below screen shot, the time part is hidden.

Show or Hide Time Part of Date Time Column using client Api

2. getShowTime() :

It checks whether the date control shows the time portion of the datetime column or not. It returns true if it shows the time portion of the date and false if not shown. So, basically, it will help to identify whether the datetime field is showing time or not.

Example: formContext.getControl(“new_dateshipped”).getShowTime();

Note: This method does not work when the DateOnly format is used.

Conclusion

With this method, you can show or hide the time component of a date control where the attribute uses the DateAndTime format.

Leave a Reply

Your email address will not be published. Required fields are marked *