Domanda

 <script>
    function numeri_validator(t) {
        var patt = /(\d*)\.{1}(\d{0,2})/;
        var donepatt = /^(\d*)\.{1}(\d{2})$/;
        var str = t.value;
        var result;
        if (!str.match(donepatt)) {
            result = str.match(patt);
            if (result != null) {
                t.value = t.value.replace(/[^\d]/gi, '');
                str = result[1] + '.' + result[2];
                t.value = str;
            } else {
                if (t.value.match(/[^\d]/gi))
                    t.value = t.value.replace(/[^\d]/gi, '');
            }
        }



       //get the date
            var val1 = document.getElementById("cost" + 1).value;
            var val2 = document.getElementById("cost" + 2).value;

       //Get date
            var date = document.getElementById("Label1").innerText;

        //format time
            var timeIn = val1.replace('.', ':');
            var timeOut = val2.replace('.', ':');

        //Concat time with date
            var timeinDate = date + " " + timeIn;
            var timeoutDate = date + " " + timeOut;

        //calculate time difference
            var nDifference = Math.abs(new Date(timeoutDate) - new Date(timeinDate));

            var one_hours = 1000 * 60 * 60;
            var hours = (Math.floor(nDifference / one_hours));

            var diff = nDifference % one_hours;

            var one_min = 1000 * 60;

            var diffmin = Math.round(diff / one_min);

            document.getElementById("datelabel").innerText = hours + "." + diffmin;

            document.getElementById("total").value = hours + "." + diffmin;

This function calculating the difference between the two times is working fine but is working only in Chrome in other browsers is:

document.getElementById("total").value=NaN:NaN

Please help me.

È stato utile?

Soluzione 2

I solved this problem by changing the date format. Actually this problem with new Date()

The new Date format should be

new Date('2013/10/19 12:00') // not 2013-10-19

Now is working on all browsers

Thanks

Altri suggerimenti

The issue should be because of this part in your code. .innerText.

var date = document.getElementById("Label1").innerText;

.innerText doesn't work in Mozilla. Use .textContent instead

var date = document.getElementById("Label1").textContent;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top