4

どの日付が選択されているかを検出できると思われるaTextBoxとa のページがあります。CalendarExtenderただし、これは選択されていない日付を報告しています。

<asp:TextBox ID="tbEffectiveDate" runat="server"
    CssClass="input-small"
    MaxLength="10"
    Text='<%# Bind("NewEffectiveDate", "{0:MM/dd/yyyy}") %>'>
</asp:TextBox>
<ajaxToolkit:CalendarExtender ID="atkEffectiveDate" runat="server"
    FirstDayOfWeek="Sunday"
    TargetControlID="tbEffectiveDate"
    Format="MM/dd/yyyy"
    OnClientDateSelectionChanged="CheckForSunday">
</ajaxToolkit:CalendarExtender>

基本的に、ユーザーが日曜日を選択していることを確認していますが、カレンダーで日を選択すると、JavaScript はそれが前日であると言います。私は困惑しています。

function CheckForSunday(sender, args) {
    var selectedDate = new Date();
    selectedDate = sender.get_selectedDate();
    // Both of these show the date before the date was selected
    alert(sender.get_selectedDate());

    if (selectedDate.getDay() != 0) {
        // not a Sunday
        var sunday = selectedDate;
        // calculated the nearest Sunday
        sunday.setDate(selectedDate.getDate() - selectedDate.getDay());
        sender.set_selectedDate(sunday);
        // tell the user that the date wasn't a Sunday
        // and that the previous Sunday was selected.
        $("#must-be-sunday").modal("show");
    }
}

たとえば、5 月 5 日などの日曜日を選択すると、次のようになります。

ここに画像の説明を入力

次に、行alert(sender.get_selectedDate());に表示されます

ここに画像の説明を入力

これは、5 月 5 日の代わりに 5 月 4 日の土曜日が選択されていることを示しています。私のロケールでは -0700 で、これは 5 日の真夜中の 7 時間前を表示しているので、これはタイムゾーンと関係があると推測しています。

これを引き起こしている可能性のあるものと、それを修正する方法を知っている人はいますか?

4

2 に答える 2

4

いつものように、ここですべてを質問に書き込んだ後、問題を解決しました。これは確かにタイムゾーンが原因でしたが、それでも非常に厄介です。誰かがより良い解決策を持っているなら、私はそれを聞きたいです.

getTimezoneOffset()とHow to add 30 minutes to a JavaScript Date object?のソリューションを使用する 、これを修正するための計算を作成しました。

var selectedDate = sender.get_selectedDate();
// get the timezone offset in minutes
var timeOffsetMinutes = selectedDate.getTimezoneOffset();
// Convert minutes into milliseconds and create a new date based on the minutes.
var correctedDate = new Date(selectedDate.getTime() + timeOffsetMinutes * 60000);

これで問題が修正され、必要な日付が得られました。

于 2013-05-08T17:09:22.493 に答える
2

CalendarExtender は各日のセル値に UTC 日付を使用するため、タイムゾーンに起因する問題は正しいことです。選択した曜日を確認したい場合は、 inハンドラの代わりにandの代わりDate.getUTCDay()に function を使用できます。Date.getDay()getUTCDate()getDate()OnClientDateSelectionChanged

于 2013-05-11T07:42:30.043 に答える