Get Time in Hours and Minutes from a Date Field in CRM using JavaScript.

D365 gives a functionality to create a Date and Time field where in you can provide the user to select the date or both date and time. But what if you just need the time in Hour and Minutes format from the selected date in the Date field, This can be achieved using a JavaScript.

In this blog lets see how we can get the time entered in the date and time field and format it in hours and minutes format.

I have created a field in CRM with field type Date and time. Here is how it looks

Using the JavaScript “Date” functions you can get the Time entered in the date and time field as shown below.

This code gets the date and time (hours and minutes) selected in the date field and will format the hour and minutes in the format “HH:MM“.

Hour – (getHour())

Minutes – (getMinutes())

Output –

Note : You can also add the AM / PM to the time string I have not added it in my time string “strTime” but have stored the value of it in “ampm“.

Hope this helps !

Share Story :