日期函数不能正常工作
date function not working proprly
在我的asp
网页中,我使用ajax
日历来选择日期。我不想让用户选择过去的日期或当前日期,或者从当前日期起超过20天。
var today = new Date();
var twentyDays = new Date();
twentyDays.setDate(today.getDate() + 20);
if (selectedDate.getDateOnly() <= todayDate.getDateOnly())
{
alert('Date Cannot be in the past or current date');
sender._textbox.set_Value(null);
}
else if (selectedDate.getDateOnly() > twentyDays.getDateOnly())
{
alert('this is more than 20 days');
sender._textbox.set_Value(null);
return;
}
但它没有比较日期。。这是我的asp代码
<asp:TextBox ID="txtDate" runat="server" ></asp:TextBox>
<asp:ImageButton ID="imgCalender" runat="server" ImageUrl="~/Images/Calendar.png" ToolTip="Select a Date" />
<asp:CalendarExtender ID="calShow" runat="server" PopupButtonID="imgCalender" PopupPosition="BottomLeft" TargetControlID="txtDate" OnClientDateSelectionChanged="CheckForPastDate"></asp:CalendarExtender>
试着去掉getDateOnly()
,据我所知,它不是javascript Date对象的一部分,并使用getTime()
我建议使用服务器端逻辑来处理这一问题,而不是JavaScript,因为当日期导致日期与今天不在同一年(即一年的12月30日)时,today.getDate() + 20
将不起作用。
相反,使用.NET Framework的DateTime
对象来添加天数,并通过ASP.NET AJAX页面方法进行比较,如下所示:
代码背后:
[WebMethod]
public static string CompareDate(string theDateToCompare)
{
DateTime dateValue;
if (DateTime.TryParse(dateString, out dateValue))
{
DateTime today = DateTime.Now;
DateTime twentyDaysFromNow = today.AddDays(20);
if(dateValue <= today)
{
return "Date cannot be in the past or current date";
}
else if (dateValue > twentyDaysFromNow)
{
return "Date cannot be more than 20 days in the future";
}
}
else
{
return "Unable to parse " + dateString;
}
}
标记:
$(document).ready(function() {
// Add selector event here to trigger call to server side
$.ajax({
type: "POST",
url: "PageName.aspx/CompareDate",
data: "{'CompareDate':'11/30/2013'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(result) {
if (result.hasOwnProperty("d")) {
// The .d is part of the result so reference it
// to get to the actual JSON data of interest
alert(result.d);
}
else {
// No .d; so just use result
alert(result);
}
}
});
});
注意:您需要将
PageName.aspx
的名称更改为.aspx页面的名称。此外,.d
语法是微软在ASP.NET AJAX的ASP.NET 3.5版本中引入的反XSS保护;因此检查CCD_ 9属性是否存在。
相关文章:
- Javascript:selenium Web驱动程序isDisplayed()不工作
- jQuery UI自动完成突然停止工作
- AngularJS UI路由器不能像ng路由器那样工作
- HTML5音频加载和播放获胜'我不能在iPad上工作
- JavaScript打印功能使日历停止工作
- Javascript.getHours()工作不正常
- 为什么这在IE中的工作方式与在Firefox中不同
- 视频HTML没有'无法在Internet Explorer 11上工作
- 扩展移相器按钮类不工作
- Firebase迁移-简单的Firebase.set没有'不再工作了——旧的还是新的
- 谷歌地图不是以HTML显示,而是在JS Fiddle上工作
- 正在尝试使用if和else添加类,但无法正常工作
- Jquery FadeIn FadeOut 只工作一次
- Foreach无法在Typescript中工作
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- 为什么不是't窗口.恢复正常工作吗?(javascript/jquery)
- JS可以在Chrome中工作,但不能在Firefox中工作
- 最大高度转换不;不工作,工作缓慢
- Gulp-rev:不是第一次工作,而是在第一次工作之后工作
- JQuery - Ajax: encodeUriComponent不工作(EncodeUri工作)