2

If i am entering a date in first text box i need to get the duration in second textbox by taking the differece with current date, i do somting its working fine only with dd/mm/yyyy format,but i need to enter the date in mm/dd/yyyy format. my working Code will be here please help me

<input type="text" name"mydate" id="mydate" placeholder="mm/dd/yyyy">
<input type="text" name="duration" id="duration">

<script>
function getYearFn()
{
from = $("#mydate").val().split("/");
var fromdate = new Date(from[2], (from[1]-1), from[0]);                         
var today = new Date();
              var year1 = fromdate.getFullYear();                
              var year2 = today.getFullYear();
              var yeardiff = year2 - year1;
              var month1 = fromdate.getMonth()+1;                  
              var month2 = today.getMonth()+1;
              var monthdiff = (month1 - month2)*-1;
              $('#duration').val(yeardiff +" " +"Years"+" "+monthdiff*-1 +""+"Months" )
}
</script>
Arif K
  • 21
  • 4
  • 1. [Date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date), 2. Adjust the parameters of `new Date(from[2], (from[1]-1), from[0])` to fit your new pattern – Andreas Jul 21 '15 at 05:33
  • possible duplicate of [How to get current formatted date dd/mm/yyyy in Javascript and append it to an input.](http://stackoverflow.com/questions/12409299/how-to-get-current-formatted-date-dd-mm-yyyy-in-javascript-and-append-it-to-an-i) – Saty Jul 21 '15 at 05:34
  • What happens if the dates are 1/2/2015 and 3/4/2014? – miniBill Jul 21 '15 at 06:15

1 Answers1

1

You only have to interchange the parameters in the date creation.
Instead of

var fromdate = new Date(from[2], (from[1]-1), from[0]);

use

var fromdate = new Date(from[2], (from[0]-1), from[1]);  
Pavel Gatnar
  • 3,987
  • 2
  • 19
  • 29