none
Disabling Past / Future dates in Sharepoint Date Picker RRS feed

  • Question

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

    found below code in msdn forum and it working fine . But for the month which has 31 dates , its not working as expected. 

    eg.On31st 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.

    Any body can help me on this

    Thanks in advance

    <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>

    Wednesday, November 6, 2019 7:10 AM

Answers

  • Hi,

    You can use Date(2019,11,31) to test 2019/12/31.

    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 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.

    • Marked as answer by Karpakam Monday, November 18, 2019 7:38 AM
    Friday, November 15, 2019 9:15 AM
    Moderator

All replies

  • Hi,

    If you want to disable the future dates, please modify the code as 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.floor(Math.abs((Date.UTC(1601, 0, 1) - Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()) ) /(1000 * 60 * 60 * 24)));
    }
    $(function(){
    	var date = new Date();	
    	var maxDay = getSPDayDiff(date);
    	var datePicker=$("img[id^='DtStartDate']").parent();
    	var onclickStr=datePicker.attr("onclick");	
    	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 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.

    Thursday, November 7, 2019 7:56 AM
    Moderator
  • Hi Dennis ,

    Thanks for your reply actually i was waiting for the response . Happy seeing your response.

    But I tried it , the problem is it didnt not worked as expected on 31st of oct. seems the prob occurs every month when the end date is 31. (Correct me if am wrong)

    eg::::On 31st, the datepicker should have been enabled until 31st but it was enabled till 1st of Nov.

    I tried to understand your above numbering to fix this but i couldnt . 

    Thanks 

    Karpakam.R

    My Exact Code is

    $(function () { 

    var currentDate = new Date();
    currentDate.setMonth(currentDate.getMonth() + 1);


    var todaysDate = new Date();
        
        var month = currentDate.getMonth()-1 ;
        var date = currentDate.getDate();
        var year =currentDate.getFullYear() ;
    var minDay = getSPDayDiff(new Date(year,month,date,0,0,0,0));
    var maxDay = getSPDayDiff(new Date(year,month,date,0,0,0,0)); 
          
      
            var datePicker1=$("input[title='IRReworkCompleteddate']").closest('tr').find("a");
    var onclickStr1=datePicker1.attr("onclick");
    onclickStr1=onclickStr1.replace("maxjday=2666269", "maxjday="+maxDay);

    datePicker1.attr("onclick",onclickStr1);    
    }); 


    • Edited by Karpakam Friday, November 8, 2019 5:24 AM
    Thursday, November 7, 2019 11:16 AM
  • Hi,

    You can use the code below to test if the date is 2019/10/31.

    <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.floor(Math.abs((Date.UTC(1601, 0, 1) - Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()) ) /(1000 * 60 * 60 * 24)));
    }
    $(function(){
    	var date = new Date(2019,9,31);	
    	var maxDay = getSPDayDiff(date);
    	var datePicker=$("img[id^='IRReworkCompleteddate']").parent();
    	var onclickStr=datePicker.attr("onclick");	
    	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 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.

    Friday, November 8, 2019 8:20 AM
    Moderator
  • Thanks for the above. I tested , its working fine when i hard coded new Date(2019,9,31).

    So hopefully the code works fine for upcoming 31'st day . 

    Thursday, November 14, 2019 12:43 PM
  • Hi,

    You can use Date(2019,11,31) to test 2019/12/31.

    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 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.

    • Marked as answer by Karpakam Monday, November 18, 2019 7:38 AM
    Friday, November 15, 2019 9:15 AM
    Moderator
  • Yeah sure . Done. Thanks for your response. 
    Monday, November 18, 2019 7:39 AM
  • Hi,

    Thanks for marking my reply as answer. I’m pleased to know that the information is helpful to you. I hope you are delighted with the service we provided you.
    Here I will provide a brief summary of this post for your information.

    Issue Symptom:

    Disabling Past/Future dates in SharePoint Date Picker

    Solution:

    Modify the code as below to solve this issue.

    <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.floor(Math.abs((Date.UTC(1601, 0, 1) - Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()) ) /(1000 * 60 * 60 * 24)));
    }
    $(function(){
    	var date = new Date();	
    	var maxDay = getSPDayDiff(date);
    	var datePicker=$("img[id^='DtStartDate']").parent();
    	var onclickStr=datePicker.attr("onclick");	
    	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 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.

    Monday, November 18, 2019 8:23 AM
    Moderator