Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Restrict on selecting dates

(0) ShareShare
ReportReport
Posted on by 80

Hello,

We have 4 DateTime fields on the form which are used to fill different sale completion dates. Let's say fields are Sale1, Sale2, Sale3, Sale4. We do not want our associates to select "future date" as a sale date. Let's say if today's date is 08/29/2021, the associate should not be able to select anything in future. All the dates in the future should be disabled(greyed out). I know we can validate the date and throw notification that future date cant be selected. I like to find out if there is a way we can grey out all future dates for the field. This we should be doing for all the four fields. Any help on this will be appreciated

  • Vishnu Kumar9 Profile Picture
    80 on at
    RE: Disable future date

    Thanks

  • Vishnu Kumar9 Profile Picture
    80 on at
    RE: Disable future date

    Thanks

  • Vishnu Kumar9 Profile Picture
    80 on at
    RE: Disable future date

    This is perfect. Thank you

  • Suggested answer
    Guido Preite Profile Picture
    54,077 Moderator on at
    RE: Disable future date

    from my previous code change this line

    var dateFieldName = "new_date1";

    to

    var dateFieldName = executionContext.getEventSource().getName();

    in this way the code can get the attribute name dynamically. Of course the function needs to be attached to the 4 field onchange events and the "Pass execution context as first parameter" checked

    hope it helps

  • Vishnu Kumar9 Profile Picture
    80 on at
    RE: Disable future date

    Hello Guido,

    Thanks for responding. Yeah seems like there is no easier way to disable those fields. I have similar query to show alert when they select future date but not to restrict future dates completely

  • Suggested answer
    Nya Profile Picture
    29,060 on at
    RE: Disable future date

    Hi,

    Please refer to the following issue in the Community of Power Apps:

    Solved: Disable the future dates in datepicker - Power Platform Community (microsoft.com)

    It cannot be greyed out.

    But an OnChange event with JavaScript on the date field could be a wise choice.

    For example, the Birthday field in Contact form should not be a future date. 

    If a future date is selected for the Birthday field, there will be an alert and the field will be empty.

    The code snippet is something like:

    var startDate = Xrm.Page.getAttribute('birthdate').getValue();
    
    var todayDate = new Date(); 
    todayDate.setHours(0,0,0);
    
    if (startDate > todayDate) 
    {
    alert("DOB is greater than today's date");
    Xrm.Page.getAttribute('birthdate').setValue(null);
    }

  • Verified answer
    Guido Preite Profile Picture
    54,077 Moderator on at
    RE: Disable future date

    it's not possible to gray out future date but you can attach a javascript function on the onchange event of the datetime field. For example:

    function CheckDate(executionContext) {
    var formContext = executionContext.getFormContext();
    var dateFieldName = "new_date1";
    var fieldValue = formContext.getAttribute(dateFieldName).getValue();
    if (fieldValue != null) {
    	var fieldValueOnlyDate = fieldValue.setHours(0,0,0,0);
    	var today = new Date();
    	today.setHours(0,0,0,0);
    	if (fieldValueOnlyDate > today) {
    		var alertMessage = { text: "Date can't be in the future" };
    		Xrm.Navigation.openAlertDialog(alertMessage);
    		formContext.getAttribute(dateFieldName).setValue(today);
    	}
    }   
    }

    note: code has not been tested and if you plan to run the same code on multiple fields it should be adapted

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

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 225 Super User 2025 Season 1

#2
Eugen Podkorytov Profile Picture

Eugen Podkorytov 106

#2
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 106 Most Valuable Professional

Overall leaderboard

Product updates

Dynamics 365 release plans