web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
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

I have the same question (0)
  • Verified answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    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

  • Suggested answer
    Nya Profile Picture
    29,064 on at

    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);
    }

  • Vishnu Kumar9 Profile Picture
    80 on at

    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
    Guido Preite Profile Picture
    54,086 Moderator on at

    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

    This is perfect. Thank you

  • Vishnu Kumar9 Profile Picture
    80 on at

    Thanks

  • Vishnu Kumar9 Profile Picture
    80 on at

    Thanks

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 128 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 97

#3
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 69 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans