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

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Restrict Past date

(0) ShareShare
ReportReport
Posted on by 365

I want to view and select  current and the future date

7115.pastedimage1608017494953v1.png

I have the same question (0)
  • Verified answer
    Community Member Profile Picture
    on at
    RE: Restrict Past date

    Hi unbeatable,

    Unfortunately, you can’t restrict use select or view past date in the Date picker of the date field directly, there is no way to achieve it.

    However, you can use javascript to date field OnChange event to prevent use filling past date in the field.

    Just like following code:

    function pastDate(executionContext) {
        var formContext = executionContext.getFormContext();
        var today = new Date();
        var date = formContext.getAttribute('new_date').getValue();
        var date1 = new Date(date);
        if (date != null) {
            if (today > date1) {
                Xrm.Page.getAttribute("new_date").setValue(null);
                alert("Sorry,You can't select past day.");
            }
        }
    }

    Test Result:

    When I fill yesterday date, it will alert you.

    7840.pastedimage1608025204181v1.png

    when you click ok button of the dialog, the date field will be cleared.

    4478.pastedimage1608025309728v2.png

    Regards,

    Leah Ju

    Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365 Forums.

  • Guido Preite Profile Picture
    54,084 Moderator on at
    RE: Restrict Past date

    a couple of corrections to Leah Ju code:

    1) this line

    var date1 = new Date(date);

    is not required, when you do a getValue against a datetime field, or is null (checked by the next line in that code) or it has a javascript Date object

    2) this line

    Xrm.Page.getAttribute("new_date").setValue(null);

    should use formContext, so formContext.getAttribute("new_date").setValue(null);

    3) this line

    alert("Sorry,You can't select past day.");

    alert works but you should use

    Xrm.Navigation.openAlertDialog({text: "Sorry,You can't select past day."});

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Pallavi Phade – Community Spotlight

We are honored to recognize Pallavi Phade as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 70 Super User 2025 Season 2

#2
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 58

#3
Anthony Blake Profile Picture

Anthony Blake 44 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans