Question: This is my date difference to calculated days code and I have user date format of `yy-dd-mm` but I have changed my format to `dd-mm-yy`.so I have changed format code, but it is not working properly.
Please can anyone help with this?

```
<script>
var calculate = function() {
    var from = document.getElementById("txtdate").value;
    var fromdate = from.slice(3, 5);
    fromdate = parseInt(fromdate);
    var frommonth = from.slice(0, 2); 
    frommonth = parseInt(frommonth);
    var fromyear = from.slice(6, 10); 
    fromyear = parseInt(fromyear);
    var to = document.getElementById("txtdate1").value;
    var todate = to.slice(3, 5); 
    todate = parseInt(todate);
    var tomonth = to.slice(0, 2); 
    tomonth = parseInt(tomonth);
    var toyear = to.slice(6, 10); 
    toyear = parseInt(toyear);
    var oneDay = 24*60*60*1000;
    var firstDate = new Date(fromyear,frommonth,fromdate);
    var secondDate = new Date(toyear,tomonth,todate);

    var diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay))+1);
    if (diffDays)
        document.getElementById("result").value=diffDays;

}
</script>

```

Answer: 
you should use from.split('-') function to get date,month,year. Then create a new Date(year,month,date) for both from and to. newTo-newFrom gives the date difference between 2 date in milliseconds. Divide this difference by 1000 \* 3600 \* 24 to get the date difference in days

```
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 

```

for reference see [Get difference between 2 dates in javascript?](https://stackoverflow.com/questions/3224834/get-difference-between-2-dates-in-javascript)
```
<html>
<head>
    <title></title>    
</head>
<body>
<input type="text" id="txtdate" value="16-04-2016" />
<input type="text" id="txtdate1" value="24-05-2016" />

<input type="text" id="result" value="" />

<!-- dd-mm-yyyy -->
<script type="text/javascript">
var calculate = function() {
    var from = document.getElementById("txtdate").value;
    var to = document.getElementById("txtdate1").value;        
    document.getElementById("result").value= process(to,from)+' Days';
}

function process(d1,d2){
var date1 = d1.split('-');
var date2 = d2.split('-');
var start = new Date(date1[2], +date1[1]-1, date1[0]);
var end = new Date(date2[2], +date2[1]-1, date2[0]);
return (start.getTime() - end.getTime()) / (1000*60*60*24);
}

calculate();

</script>   
</body>
</html>

```

**[Fiddle](https://jsfiddle.net/mohammedshafeek/q8kzu7f3/)**