Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

Time-Only Input on Model-Driven App

(1) ShareShare
ReportReport
Posted on by 5
Hi,
 
On my Model-Drive app, I need to be able to allow the user to input time only (hh:mm am/pm). I cannot find an out of the box way to achieve this. I found a couple of custom pcf components, but they store the time in multiple numeric columns (hours, minutes, seconds). After that, I want to be able to store the time in another column as 1/1/1900 hh:mm:ss (24 hour time format).
 
How can this be achieved?
 
Any assistance is appreciated!
  • ankeet1pi9 Profile Picture
    5 on at
    Time-Only Input on Model-Driven App
    NOT RESOLVED YET
     
    UPDATE: I mentioned in my original post that I tried using PCF controls. Here's one that I was referring to - https://pcf.gallery/time-picker/
  • Suggested answer
    Daivat Vartak (v-9davar) Profile Picture
    2,264 Super User 2025 Season 1 on at
    Time-Only Input on Model-Driven App
    Hello Ankeet,
     

    You've identified a common challenge: Dynamics 365 doesn't have a direct "time-only" field type. You're also right to be concerned about the limitations of some PCF controls that split the time into multiple columns. Let's explore how to achieve your desired outcome:

    Understanding the Challenge:

    • No Time-Only Field: Dynamics 365 stores date and time together.

    • PCF Limitations: Some PCF controls don't store time in a standard format.

    • Desired Format: You need to store the time as 1/1/1900 hh:mm:ss (24-hour format) in a DateTime field.

    •  

    Solutions:

    1. Custom PCF Control (Recommended):

      • Create a Custom Time Picker PCF:

        • Develop a custom PCF control that provides a user-friendly time picker interface.

        • The control should:

          • Allow users to select time in hh:mm am/pm format.

          • Convert the selected time to 1/1/1900 hh:mm:ss (24-hour format).

          • Store the formatted time in a DateTime field. 

        • This is the most user friendly method. 

      • Advantages:

        • Provides a seamless user experience.

        • Stores the time in the desired format.

        • You will have complete control over the input, and output. 

      • Considerations:

        • Requires PCF development skills.

        • Requires testing and maintenance.

        •  

    2. JavaScript and a DateTime Field:

      • Add a DateTime Field:

        • Add a DateTime field to your entity. 

      • Create a Time Input Field (HTML):

        • Add a single line of text field to the form.

        • Then use JavaScript to change the single line of text field into a HTML time input field. 

      • JavaScript Logic:

        • Use JavaScript to:

          • Capture the time entered by the user.

          • Convert the time to 24-hour format.

          • Construct a DateTime string in the format 1/1/1900 hh:mm:ss.

          • Set the value of the DateTime field. 
           

      • Advantages:

        • No need for a PCF control.

        • Provides more control over the input. 

      • Considerations:

        • Requires JavaScript skills.

        • Can be complex.

        • Requires more testing.

        • HTML input controls can have cross browser compatibility issues. 
         

    3. Two Numeric Fields and a Calculated Field:

      • Add Numeric Fields:

        • Add two numeric fields to your entity: "Hours" and "Minutes." 

      • Add a Calculated Field:

        • Add a calculated DateTime field.

        • Then use the calculated field to combine the hours and minutes into the desired date time format. 

      • JavaScript (Optional):

        • Use javascript to help with user input validation. 

      • Advantages:

        • No need for a PCF control.

        • Relatively simple to implement. 

      • Disadvantages:

        • Less user-friendly than a time picker.

        • Requires manual input of hours and minutes.

        • Does not have the am/pm functionality.

        •  

    4.  

    Example JavaScript (Conceptual):

    function setTime(executionContext) {
        var formContext = executionContext.getFormContext();
        var timeInput = formContext.getAttribute("your_time_input_field").getValue(); // Get the time string
        if (timeInput) {
            // Convert time to 24-hour format and construct DateTime string
            var timeParts = timeInput.split(":");
            var hours = parseInt(timeParts[0]);
            var minutes = parseInt(timeParts[1]);
            var ampm = timeParts[2].toLowerCase();
            if (ampm === "pm" && hours < 12) {
                hours += 12;
            } else if (ampm === "am" && hours === 12) {
                hours = 0;
            }
            var dateTimeString = "1/1/1900 " + hours.toString().padStart(2, '0') + ":" + minutes.toString().padStart(2, '0') + ":00";
            formContext.getAttribute("your_datetime_field").setValue(new Date(dateTimeString));
        }
    }

     

    Key Recommendations:

    • Custom PCF Control: This is the most user-friendly and robust solution.

    • JavaScript and DateTime Field: This is a viable option if you're comfortable with JavaScript.

    • Two Numeric Fields and Calculated Field: This is the simplest option, but it's less user-friendly.

    •  

    By using these solutions, you can effectively implement time-only input and store the time in your desired format.

     
    If my answer was helpful, please click Like, and if it solved your problem, please mark it as verified to help other community members find more. If you have further questions, please feel free to contact me.
     
    My response was crafted with AI assistance and tailored to provide detailed and actionable guidance for your Microsoft Dynamics 365 query.
     
    Regards,
    Daivat Vartak

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,815 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,707 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans