I'm encountering an issue on a webpage where I have a TextBox
and a CalendarExtender
. The purpose of this setup is to help me determine the selected date. However, it's showing the wrong date.
<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>
The issue arises when trying to ensure that a Sunday is selected by the user. Even though I click on a day in the calendar, the JavaScript function reports the previous day. It's quite perplexing.
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");
}
}
When selecting a date like Sunday, May 5th:
Upon reaching
alert(sender.get_selectedDate());
, it displays
The displayed date is Saturday, May 4th instead of May 5th. My assumption is that since my timezone is -0700 and it shows 7 hours prior to midnight on the 5th, the time zone might be the root cause of this issue.
If anyone has insights into what could be causing this discrepancy and suggestions for resolving the problem without factoring in the time, please let me know.