0

I am trying to know if a date has already past-by and if it has I would make the input box non-editable.

My input box code is:

<input id="inserviceInputId" type="text" value="09/05/2003" name="inserviceInput" style="width:7em">

and the Dojo code I have so far:

var date1 = new Date;
    dojo.query("[name=inserviceInput]").forEach(function(evnt,i){

    console.log(evnt.value)

    });

So, I do get the input box's value here. But the issue is how to subtract the today's date and the date in the value.

Thanks for the help in advance.

Zeeshan Rang
  • 19,375
  • 28
  • 72
  • 100

2 Answers2

0

first, I don't think your syntax is correct. It looks like you are trying to write an event handler ( function(evnt, i) ) instead of doing some action on the value from the node.

I attached some corrected code below, and to explain I added inline comments:

dojo.query("input[name='inserviceInput']").forEach(function(node){
   var date1 = new Date();  // get the current date and time

   var date2 = new Date(node.value); // turn the input from the input field into a date

   if (date1>date2) { // compare the two dates, 

     //if the current date is larger than the date from the input field... 
     // do something here  
   } 

});

Iwan
  • 181
  • 3
  • You code is working fine, just one issue is in, my date values in the input box is in dd/mm/yyyy format. I believe because of which the conversion is not happening correct. And I am getting a few incorrect answers also. – Zeeshan Rang Feb 19 '13 at 17:01
  • I think the answer below addresses that. One thing I'd look into is using a date picker instead of relying on people to put the correct date format in, or you will be in a date conversion nightmare. – Iwan Feb 19 '13 at 17:18
0

Use the dojo/date/locale to parse the value into a javascript Date object and then just compare the date objects.

require(["dojo/query", "dojo/date/locale"], function(query, locale){

  query("[name=inserviceInput]").forEach(function(node){

    var dtNow = new Date();
    var dtValue = locale.parse(node.value, {
        selector: 'date',
        datePattern: 'dd/MM/yyyy'
    });

    // validation
    if(dtValue < dtNow) {
      // error condition...
    }

  });
});

Compare two dates with JavaScript

Community
  • 1
  • 1
Craig Swing
  • 8,152
  • 2
  • 30
  • 43