0

I have some problem with IE (all versions). It don't calculating the code of timestamp. My code is below:

function compute() {
    var c = $('select#time_from_hour').val();
    var d = $('select#time_from_minute').val();
    var e = $('select#time_to_hour').val();
    var f = $('select#time_to_minute').val();
    var g = $('input#date_from').val();
    var h = $('input#date_to').val();
    var hour1 = g + ' ' + c + ':' + d; 
    var hour2 = h + ' ' + e + ':' + f; 

    hour1 = hour1.split("/"); 
    var hour1 = hour1[1] + "," + hour1[0] + "," + hour1[2];
    var timestamp = (new Date(hour1).getTime() / 1000) + 7200;
    $('#total_hour1').val(timestamp);

    hour2 = hour2.split("/"); 
    var hour2 = hour2[1] + "," + hour2[0] + "," + hour2[2];
    var timestamp = (new Date(hour2).getTime() / 1000) + 7200;
    $('#total_hour2').val(timestamp);

    var x = -$('input#total_hour1').val();
    var y = -$('input#total_hour2').val();
    var total_time = x - y; 
    result = total_time / 86400;
    new_number = Math.ceil(result); 

    if (isNaN(new_number)) { 
        var new_number = 0;
    }

    $('#finish_day').val(new_number);
}

$('select#time_from_hour').change(compute);
$('select#time_from_minute').change(compute);
$('select#time_to_hour').change(compute);
$('select#time_to_minute').change(compute);
// $('select#return_car').change(compute);
$('select#get_car').change(compute);
$('input#finish_day').change(compute);

On other modern browser (firefox, opera, chrome) everything works fine. Only on IE i got the "NaN" value in timestamp1 and timestamp2.

Thanks in advice.

4

3 回答 3

1

You seem to pass the Argument of the Date-Constructor in the wrong format. According to MSDN, the IE-Implementation of Date accepts the Date in the following order:

function Date( year : int, month : int, date : int[, hours : int [, minutes : int [, seconds : int [, ms : int]]]] )

If you make sure, that you pass the arguments in this format, everything should work fine. You'll find the working Code here: http://jsfiddle.net/utXMD/

于 2012-08-06T12:37:24.487 回答
0

Got the problem and able to run your code in IE too.

var hour1 = g + ' ' + c + ':' + d; 
var hour2 = h + ' ' + e + ':' + f; 

hour1 = hour1.split("/"); 
var hour1 = hour1[1] + "," + hour1[0] + "," + hour1[2];
var timestamp = (new Date(hour1).getTime() / 1000) + 7200;

No need to split the hour1, just pass the same to calculate the timestamp. below code works

var hour1 = g + ' ' + c + ':' + d;
var hour2 = h + ' ' + e + ':' + f;

//    var hour1 = hour11.split("/");
//  hour1 = hour1[1]+","+hour1[0]+","+hour1[2];
var timestamp = (new Date(hour1).getTime()/1000) + 7200;
$('#total_hour1').val(timestamp);
于 2012-08-06T12:28:39.073 回答
0

In your code:

> function compute() {
>     var c = $('select#time_from_hour').val();
>     var d = $('select#time_from_minute').val();
>     var e = $('select#time_to_hour').val();
>     var f = $('select#time_to_minute').val();
>     var g = $('input#date_from').val();
>     var h = $('input#date_to').val();
>     var hour1 = g + ' ' + c + ':' + d;
>     var hour2 = h + ' ' + e + ':' + f; 
>
>     hour1 = hour1.split("/"); 
>     var hour1 = hour1[1] + "," + hour1[0] + "," + hour1[2];

Presumably the above results in a string something like:

day/month/year h:m:s

which is not guaranteed to be parsed correctly by any browser. In ECMA-262 ed 3 the parsing of date strings was entirely implementation dependent. In ES5 a modified version of the ISO8601 format is specified but it's not supported by all browsers. And the above string isn't compliant with that format either.

>     var timestamp = (new Date(hour1).getTime() / 1000) + 7200;

That seems to be an attempt at creating a seconds since epoch number from a timestamp. It's better to use the bits of date and time you already have, something like:

// Presuming g is in the format day/month/year
g = g.split("/");
var someDate = new Date(+g[2], --g[1], +g[0], +c, +d + 2); // add 2 hours here
var timestamp = Math.round(someDate/1000);

I suggest you have a look at how the Date constructor works and use it directly rather than trying to create a string that must them be parsed.

于 2012-08-06T12:35:42.213 回答