简体   繁体   English

CalendarExtender说选择了错误的日期,可能与时区有关

[英]CalendarExtender saying the wrong date is selected, possibly timezone related

I have a page with a TextBox and a CalendarExtender that is supposed to allow me to detect what date is selected. 我有一个带有TextBoxCalendarExtender的页面,该页面应该允许我检测选择了什么日期。 However, this is reporting the date that isn't selected. 但是,这将报告未选择的日期。

<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>

Essentially I'm making sure the user has selected a Sunday, but when I select a day on the calendar, the JavaScript says it is a day before. 本质上,我要确保用户选择了星期日,但是当我在日历上选择某天时,JavaScript会说它是前一天。 I'm perplexed. 我很困惑。

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");
    }
}

For example, if I select a Sunday, such as May 5th: 例如,如果我选择一个星期日,例如5月5日:

在此处输入图片说明

Then at the line alert(sender.get_selectedDate()); 然后在该行中alert(sender.get_selectedDate()); , it displays ,它显示

在此处输入图片说明

This is saying Saturday, May 4th is selected instead of May 5th. 也就是说,选择的是5月4日(星期六),而不是5月5日。 Since in my locale, we are -0700 and this is displaying 7 hours before midnight on the 5th, I'm guessing this has something to do with the timezone. 由于在我的语言环境中,我们的时间是-0700,这是在5日午夜7时之前显示的时间,因此我猜测这与时区有关。

Does anyone know what may be causing this and how to fix it so it doesn't work with the time and only the date selected? 有谁知道这可能是什么原因以及如何解决此问题,从而使其不适用于时间和所选日期?

As usual, after writing everything out into a question on here, I've resolved my issue. 和往常一样,在此处将所有内容写成问题后,我已经解决了问题。 This was indeed due to timezones, but still is very awkward. 这确实是由于时区造成的,但仍然很尴尬。 If someone has a better solution, I'd love to hear it. 如果有人有更好的解决方案,我很想听听。

Using getTimezoneOffset() and the solution from How to add 30 minutes to a JavaScript Date object? 使用getTimezoneOffset()如何添加30分钟到JavaScript Date对象中的解决方案 , I created a calculation to fix this. ,我创建了一个计算来解决此问题。

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);

This corrected my issue and I get the date needed. 这解决了我的问题,我得到了所需的日期。

You right that the problem due to timezones as CalendarExtender use UTC dates for each day cell value. 您认为由于CalendarExtender的时区而导致的问题对每个日期单元格值都使用UTC日期。 If you want to check day of week selected you may use Date.getUTCDay() function instead of the Date.getDay() and getUTCDate() instead of getDate() in OnClientDateSelectionChanged handler. 如果要检查所选的星期几,可以在OnClientDateSelectionChanged处理程序中使用Date.getUTCDay()函数代替Date.getDay()getUTCDate()代替getDate()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM