How to make an input box enable for only past dates and for the startdate if it falls in the current month?

Joined
Oct 24, 2013
Messages
47
Reaction score
0
how to make an input box enable for only past dates and for the startdate that if it falls in the current month in JavaScript.
The input box should be disabled for future dates and for the startdate if it falls in the current month.
I have implemented the function but not sure the following logic will work for every scenario.

JavaScript:
((new Date(startDate)).getMonth() === new Date().getMonth() || startDate > new Date())

const globalPendingStatusIDL3 = "76070";
      
const check_IsExternalUser_And_PendingStatusL3  = (statusID = null, startDate = null) => {       
    return !!statusID && !!startDate ? (statusID === "76070" && ((new Date(startDate)).getMonth() === new Date().getMonth() || startDate > new Date())) : false;
};

//Code where the input box is being created and disabled being set using a loop:
strHTML += "<td width=\"100px\"><input class=\"form-control target-form\" data-recordid=\"" + recordid + "\" data-oldvalue=\"" + actualValue + "\" id=\"txtActualValue_" + recordid + "\" data-prevrecordid=\"" + (prevrecordid == "" ? "" : ("txtActualValue_" + prevrecordid)) + "\" value=\"" + actualValue + "\"" + (globalIsStaff ? "" : (check_IsExternalUser_And_PendingStatusL3(statusID,startDate) === true ? "disabled" : "")) + "/></td>";

//Few scenarios and the desired result:
check_IsExternalUser_And_PendingStatusL3("76070",new Date("2022-12-01")); //The input box should be enable
check_IsExternalUser_And_PendingStatusL3("76070",new Date("2023-01-01")); //The input box should be enable
check_IsExternalUser_And_PendingStatusL3("76070",new Date("2023-06-01")); //The input box should be disable
check_IsExternalUser_And_PendingStatusL3("76070",new Date("2023-06-30")); //The input box should be disable
check_IsExternalUser_And_PendingStatusL3("76070",new Date("2023-07-01")); //The input box should be disable
check_IsExternalUser_And_PendingStatusL3("76070",new Date("2024-01-01")); //The input box should be disable
 
Last edited:
Joined
Sep 4, 2022
Messages
136
Reaction score
16
Html 5 have modern input for date,
which can be set with a 'high limit' Date, and a start Date condition : try it !
It's 'min' and 'max' attribute

read here, you'll understand :
 
Joined
Oct 24, 2013
Messages
47
Reaction score
0
Html 5 have modern input for date,
which can be set with a 'high limit' Date, and a start Date condition : try it !
It's 'min' and 'max' attribute

read here, you'll understand :
I just want to make sure that my function will work in every scenario, which will make the input box disabled.

const check_IsExternalUser_And_PendingStatusL3 = (statusID = null, startDate = null) => {
return !!statusID && !!startDate ? (statusID === "76070" && ((new Date(startDate)).getMonth() === new Date().getMonth() || startDate > new Date())) : false;
};
 
Joined
Jul 4, 2023
Messages
502
Reaction score
62
Try in that way:

JavaScript:
// Get today's date
var last_month = new Date();
var today = new Date();

// Set the date to the last day of last month
last_month.setMonth(last_month.getMonth(), 0);

// Get milliseconds
console.log(last_month.getTime());
console.log(today.getTime());


JavaScript:
const check_IsExternalUser_And_PendingStatusL3  = (statusID = null, startDate = null) => {
  const last_month = new Date();
  last_month.setMonth(last_month.getMonth(), 0);
  return !(!!statusID && (last_month.getTime() >= startDate.getTime()));
}

Full demo: on-line ;)
 
Joined
Jul 4, 2023
Messages
502
Reaction score
62
Try in that way:

JavaScript:
// Get today's date
var last_month = new Date();
var today = new Date();

// Set the date to the last day of last month
last_month.setMonth(last_month.getMonth(), 0);

// Get milliseconds
console.log(last_month.getTime());
console.log(today.getTime());

JavaScript:
const check_IsExternalUser_And_PendingStatusL3  = (statusID = null, startDate = null) => {
  const last_month = new Date();
  last_month.setMonth(last_month.getMonth(), 0);
  return !(!!statusID && (last_month.getTime() >= startDate.getTime()));
}


Full demo:

HTML:
<!DOCTYPE html>
<html>
  <head>
    <style>
      input {
        display: block;
      }
      input + input {
        margin-top: .5em;
      }
    </style>
  </head>
  <body>

    <script>
      const globalPendingStatusIDL3 = "76070";

      const check_IsExternalUser_And_PendingStatusL3  = (statusID = null, startDate = null) => {
        const last_month = new Date();
        last_month.setMonth(last_month.getMonth(), 0);
        return !(!!statusID && (last_month.getTime() >= startDate.getTime()));
      }

      // Few scenarios and the desired result:
      const demo_date = [
        '2022-12-01', '2023-01-01', '2023-06-01', '2023-06-30', '2023-07-01', '2024-01-01'
      ];

      window.onload = load;

      function load() {
        const html = document.createDocumentFragment();

        for (const date_item of demo_date) {
          const input = document.createElement('input');
          input.value = date_item;
          input.disabled = check_IsExternalUser_And_PendingStatusL3(globalPendingStatusIDL3, new Date(date_item));
          html.appendChild(input);
        }

        document.body.appendChild(html); 
      }
    </script>
  </body>
</html>
 
Joined
Oct 24, 2013
Messages
47
Reaction score
0
Try in that way:

JavaScript:
// Get today's date
var last_month = new Date();
var today = new Date();

// Set the date to the last day of last month
last_month.setMonth(last_month.getMonth(), 0);

// Get milliseconds
console.log(last_month.getTime());
console.log(today.getTime());

JavaScript:
const check_IsExternalUser_And_PendingStatusL3  = (statusID = null, startDate = null) => {
  const last_month = new Date();
  last_month.setMonth(last_month.getMonth(), 0);
  return !(!!statusID && (last_month.getTime() >= startDate.getTime()));
}


Full demo:

HTML:
<!DOCTYPE html>
<html>
  <head>
    <style>
      input {
        display: block;
      }
      input + input {
        margin-top: .5em;
      }
    </style>
  </head>
  <body>

    <script>
      const globalPendingStatusIDL3 = "76070";

      const check_IsExternalUser_And_PendingStatusL3  = (statusID = null, startDate = null) => {
        const last_month = new Date();
        last_month.setMonth(last_month.getMonth(), 0);
        return !(!!statusID && (last_month.getTime() >= startDate.getTime()));
      }

      // Few scenarios and the desired result:
      const demo_date = [
        '2022-12-01', '2023-01-01', '2023-06-01', '2023-06-30', '2023-07-01', '2024-01-01'
      ];

      window.onload = load;

      function load() {
        const html = document.createDocumentFragment();

        for (const date_item of demo_date) {
          const input = document.createElement('input');
          input.value = date_item;
          input.disabled = check_IsExternalUser_And_PendingStatusL3(globalPendingStatusIDL3, new Date(date_item));
          html.appendChild(input);
        }

        document.body.appendChild(html);
      }
    </script>
  </body>
</html>
Txs. It works. Appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,034
Messages
2,570,356
Members
47,002
Latest member
RobertoLip

Latest Threads

Top