Get Month | Day | Year 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 need just the month from the selected date in the date field or say the date or year from the selected date in the date field, This can be achieved using a JavaScript.

In this blog lets see how we can split or get the Month | Day | Year from a selected date in the CRM Date and time type field.

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 Month | Day | Year and Time of the entered date as shown in the below script.

This code gets the date selected in the date field and splits the date into

Month – (getMonth()) Note : January – December is (0-11) hence add 1 so make it (1-12).

Day – (getDate())

Year – (getFullYear())

This code gives the month date and date in 2 digit format i.e. if the date is a single digit say 9 it will give it as 09.

You can further use this according to your requirement either to change the date format or for many other date related customizations.

Hope this helps !

Share Story :