0

I have been developing a new website and have come across a strange datetime problem which I cannot seem to work out what is wrong.

My staging server is here: Link

As you can see, people can enquire about taxis "Now" or in the "Future".

What should happen is that if you chose "Future" and put in a date/time more than 2 hours ahead then I will be able to give you live quotes for taxis. If the lead time is less than 2 hours ahead then you should not see any "live" quotes.

I am developing it based on an API from a company in Finland and they have been testing it as well.

The guys in Finland are reporting that even when they select "Now" they are still getting live quotes when they shouldn't.

I use some JavaScript to populate the datetime selectors on the page like this:

var d = new Date();
var curr_hour = d.getHours(); // this is in 24 hour
var curr_minute = d.getMinutes();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();

if (curr_minute < 15) { $("#enquiry_timeMins").val(15); }
if (curr_minute >= 15 && curr_minute < 30) { $("#enquiry_timeMins").val(30); }
if (curr_minute >= 30 && curr_minute < 45) { $("#enquiry_timeMins").val(45); }
if (curr_minute >= 45 && curr_minute < 59) { $("#enquiry_timeMins").val(00); curr_hour = curr_hour + 1; }

if (curr_hour >= 12) {
    $("#enquiry_timeAMPM").val("PM");
    $("#enquiry_timeHours").val(curr_hour - 12);
}
else {
    $("#enquiry_timeAMPM").val("AM");
    $("#enquiry_timeHours").val(curr_hour);
}

$("#enquiry_pickupTime").val(curr_date + "/" + curr_month + "/" + curr_year);

and then in my controller I do this:

theRoute.enquiry.pickupTime = theRoute.enquiry.pickupTime.AddHours(theRoute.enquiry.timeHours);
theRoute.enquiry.pickupTime = theRoute.enquiry.pickupTime.AddMinutes(theRoute.enquiry.timeMins);
if (theRoute.enquiry.timeAMPM == "PM") { theRoute.enquiry.pickupTime = theRoute.enquiry.pickupTime.AddHours(12); }

This seems to work perfectly for me but I'm based in the UK. The guys in Finland happen to be 2 hours ahead as well. Which seems more than a coincidence as the lead time is also 2 hours ahead.

Can anyone see anything that I haven't considered that makes this work in the UK but not in Finland, and possibly other countries as well?

4

2 に答える 2

0

わかった。私はそれを修正したと思います...

これは私がやったことです。

ブラウザの時間の前後の時間数を知る必要があることがわかったので、その後、照会時間を前後にどれだけ移動する必要があるかを計算できます...

この要点は、時差と夏時間の計算に役立つことがわかりました。

function getTimeZoneOffsetDST() {
    // NOTE: return new Date().getTimezoneOffset() is not enought !
    var today = new Date();
    // 2nd Sunday in March can't occur after the 14th :
    var dstBeg = new Date("March 14, " + today.getFullYear() + " 02:00:00");
    // 1st Sunday in November can't occur after the 7th :
    var dstEnd = new Date("November 07, " + today.getFullYear() + " 02:00:00");
    dstBeg.setDate(14 - dstBeg.getDay()); // Calculate second Sunday in March
    dstEnd.setDate(7 - dstEnd.getDay()); // Calculate first Sunday in November
    if (today >= dstBeg && today < dstEnd) { // isDST
        // e.g. for GMT+02:00 returns -120 !
        return today.getTimezoneOffset() + 60;
    }
    else {
        return today.getTimezoneOffset();
    }
}

このようなjqueryのビット

$("#timeMinsOffset").val(getTimeZoneOffsetDST());

これを入力します:

@Html.HiddenFor(model2 => Model.enquiry.timeMinsOffset, new { id = "timeMinsOffset" })

コントローラーに戻って、次のようにオフセット時間を追加するだけです。

// at this point theRoute.enquiry.pickupTime is a null DateTime
theRoute.enquiry.pickupTime = theRoute.enquiry.pickupTime.AddHours(theRoute.enquiry.timeHours);
theRoute.enquiry.pickupTime = theRoute.enquiry.pickupTime.AddMinutes(theRoute.enquiry.timeMins);
theRoute.enquiry.pickupTime = theRoute.enquiry.pickupTime.AddMinutes(theRoute.enquiry.timeMinsOffset);
if (theRoute.enquiry.timeAMPM == "PM") { theRoute.enquiry.pickupTime = theRoute.enquiry.pickupTime.AddHours(12); }

振り返ってみると明らかです。新人え?

これが他の誰かに役立つことを願っています!

于 2013-05-22T18:03:37.290 に答える