0

I have two date Picker one is for FromDate and another is for ToDate and i want to subtract both date so that i can get DateDiffrence using j Query I understand i can not store two Selected date in variable and then i can perform Subtraction.

 <script>
        function deFromDateChanged(s, e) {
            var deFromDate = (s.GetInputElement().value)
            alert(deFromDate );
        }
         function deToDateChanged(s, e) {
            var deToDate = (s.GetInputElement().value)
            alert(deToDate );
        }
     var ResultDate = deToDate  - deFromDate  //That is Wrong I can Not access

    </script>

DateEdit

  <td>Issues Date:</td>
    <td>
      <dx:ASPxDateEdit ID="deFromDate" runat="server" Width="220px" AllowUserInput="False" Height="25px" EditFormatString="dd-MMM-yyyy" DisplayFormatString="dd-MMM-yyyy" EditFormat="Custom"
          Theme="Office2003Blue" ClientInstanceName="deFromDate" OnInit="deFromDate_Init">
           <DropDownButton Image-Url="../Images/calendar.png" Image-Height="23px" Image-Width="23px"></DropDownButton>
            <ClientSideEvents DateChanged="deFromDateChanged" />
              </dx:ASPxDateEdit>
                </td>
                  <td>
      <dx:ASPxDateEdit ID="deToDate" runat="server" Width="220px" AllowUserInput="False" Height="25px"
           EditFormatString="dd-MMM-yyyy" DisplayFormatString="dd-MMM-yyyy" EditFormat="Custom"
           Theme="Office2003Blue" ClientInstanceName="deQuoDate" OnInit="deToDate_Init">
             <DropDownButton Image-Url="../Images/calendar.png" Image-Height="23px" Image-Width="23px"></DropDownButton>
               <ClientSideEvents DateChanged="deToDateChanged" />
                </dx:ASPxDateEdit>
                 </td>

As i can understand i cn not access both Date value in one function is there any alternate way to do same

cd d
  • 73
  • 6
  • `I understand i can not store two Selected date in variable and then i can perform Subtraction` I'm not sure why you think this, as you can do that and in fact it's exactly what you need to do to determine the difference between the dates. – Rory McCrossan Nov 03 '20 at 09:28
  • 1
    So your question is "how to find the difference between two dates in Javascript"? Would you think that this might already be answered somewhere? – Tomalak Nov 03 '20 at 09:28
  • @RoryMcCrossan Please Check my Updated Question Can i Access both variable ? – cd d Nov 03 '20 at 09:31
  • @Tomalak I want "how to find the difference between two dates in Javascript"? but i am not able to Retrieve value from dx:ASPxDateEdit Is there any alternate way to achieve same – cd d Nov 03 '20 at 09:32
  • @cdd Ah, understood. – Tomalak Nov 03 '20 at 09:38

1 Answers1

2

Your deFromDate and deToDate variables are stored locally in your functions.

To use the variables outside the function you have to inizialize them outside:

var deToDate;

var deToDate;

Then assign the value inside the functions

function deFromDateChanged(s, e) {
        deFromDate = (s.GetInputElement().value)
        alert(deFromDate );
    }  

function deToDateChanged(s, e) {
        deToDate = (s.GetInputElement().value)
        alert(deToDate );
    }

and then subtract two date object

var ResultDate = Math.abs(deToDate - deFromDate); //difference in milliseconds
Andrea Viviani
  • 217
  • 1
  • 6
  • You still need to introduce a callback for the *"and then"* case. – Tomalak Nov 03 '20 at 09:40
  • Why its showing On page load NaN? I modified my code as you Explain but on page load its showing Nan – cd d Nov 03 '20 at 09:52
  • Data/time are not numbers so you cant subtract look at this: https://stackoverflow.com/questions/4944750/how-to-subtract-date-time-in-javascript – Andrea Viviani Nov 03 '20 at 10:02