0

I have written below function which calculate date difference in days

function dateDiff  (date2, date1) {
        var days = 0;
        if (date2 != null && date1 != null) {
            date1 = new Date(date1).getTime();
            date2 = new Date(date2).getTime();
            var timediff = date2 - date1;

            if (!isNaN(timediff)) {
                //day 86400000 = second = 1000,minute = second * 60,hour = minute * 60,day = hour * 24, to get day
                days = Math.floor(timediff / 86400000);
            }
        }

        return days > 0 ? days : 0;
    };

then i have called this function with following parameter

dateDiff (new Date,'2013-09-17T00:00:00') //return 1 days 
dateDiff (new Date,'2013-09-17T17:31:57.75' ) //return 0 days 

value of new Date() = 'Wed Sep 18 2013 18:50:01 GMT+0530 (India Standard Time)'

this function work fine in firefox but not work in chrome? i am not getting why this function return different result in different browser?

Shivkumar
  • 1,903
  • 5
  • 21
  • 32

2 Answers2

0
date1 = '05/06/2010';
date2 = '01/07/2011';
var date_1= date1.split('/');
var date_2= date2.split('/')

date_new1 = Date(date_1[2], date_1[0]-1, date_1[1]);
date_new2 = Date(date_2[2], date_2[0]-1, date_2[1]);

date_difference = (date_new2 - date_new1)/(1000*60*60*24);

$("#Tag_ID").val(date_difference);
Ali Haider
  • 92
  • 2
0

Use the excellent library moment.js. Given 2 dates (or date strings):

d1 = moment("2013-09-11");
d2 = moment("2013-09-01");
dur = d1.diff(d2, "days");
console.log(dur); // 10
Nikos Paraskevopoulos
  • 39,514
  • 12
  • 85
  • 90