none
SharePoint datepicker - Restrict past dates and current date + 1 month in custom list javascript client object model RRS feed

  • Question

  • Hi,

    I have a custom list, in which I have added a date picker control. I have a requirement in which user should not be able to select past date and also "current date + 1 month" .

    How to restrict this using Javascript client object model?

    Thanks

    Monday, April 17, 2017 5:40 AM

All replies

  • Hi,

    We can use jQuery to achieve it. Add the code below into a content editor web part in list new/edit form page.

    <script src="//code.jquery.com/jquery-3.2.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    var getSPDayDiff = function(date) {
    	return Math.round(Math.abs(( new Date(1601,0,1).getTime() - date.getTime())/( 24*60*60*1000 ) ) );
    }
    $(function () { 
    
    	var currentDate = new Date();
    	currentDate.setMonth(currentDate.getMonth() + 1);
    	var minDay = getSPDayDiff(new Date());
    	var maxDay = getSPDayDiff(currentDate); 
    	
    	var datePicker=$("img[alt='Select a date from the calendar.']").parent();
    	var onclickStr=datePicker.attr("onclick");
    	onclickStr=onclickStr.replace("minjday=109207", "minjday="+minDay);
    	onclickStr=onclickStr.replace("maxjday=2666269", "maxjday="+maxDay);
    	datePicker.attr("onclick",onclickStr);
    });
    </script>
    

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, April 18, 2017 3:47 AM
    Moderator
  • Hi,

    Thanks for the reply.

    If it is for a specific date picker (example: DtStartDate), how to use the above code?

    Also, the conditions are:

    1) No past dates

    2) User can only select dates starting from 1 month from today. I mean, If current date is 18th April, then user can select any date starting from 19th May

    Thanks


    • Edited by Venkatzeus Tuesday, April 18, 2017 10:20 AM
    Tuesday, April 18, 2017 10:13 AM
  • Hi,

    If the date field name is "DtStartDate", please use the code below:

    <script src="//code.jquery.com/jquery-3.2.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    var getSPDayDiff = function(date) {
    	return Math.round(Math.abs(( new Date(1601,0,1).getTime() - date.getTime())/( 24*60*60*1000 ) ) );
    }
    $(function () { 
    
    	var currentDate = new Date();
    	currentDate.setMonth(currentDate.getMonth() + 1);
    	var minDay = getSPDayDiff(new Date());
    	var maxDay = getSPDayDiff(currentDate); 
    	
    	var datePicker=$("img[id^='DtStartDate']").parent();
    	var onclickStr=datePicker.attr("onclick");
    	onclickStr=onclickStr.replace("minjday=109207", "minjday="+minDay);
    	onclickStr=onclickStr.replace("maxjday=2666269", "maxjday="+maxDay);
    	datePicker.attr("onclick",onclickStr);
    });
    </script>

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, April 19, 2017 1:17 AM
    Moderator
  • Hi,

    Any update?

    If my reply helps you, please mark the reply as answer, it will make others who stuck with the similar issue easier to search for valid solutions in this forum.

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, May 4, 2017 5:26 AM
    Moderator
  • Hi Dennis,

    I have a requirement to disable future date, user should be able to choose only past and current date

    Your above code is working fine , thanks for that. But for the month which has 31 dates , its not working as expected. 

    eg. today the datepicker should be enabled until 31st oct but it enabled 1st Nov also . Please help me to fix this.

    The issue occurs when i disable future date as well , here its enabled till 1st of Nov instead 31st oct. Your assistance on this will be much appreciated . Thanks in advance

    Karpakam.R


    Thursday, October 31, 2019 5:58 PM
  • Hi Dennis,

    Can you able to help me on above request or

    any body in the forum 

    Many Thanks 

    Wednesday, November 6, 2019 7:06 AM
  • Hi Karpakam,

    I would suggest you post it in a new thread, it would make others easier to focus on one question in one single thread and it will benefit other community members who stuck with the same question.

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Wednesday, November 6, 2019 9:57 AM
    Moderator