前台
<td height="25" width="*" align="left">
<asp:TextBox ID="txtACTIVITYTIME" runat="server" Width="100px" onfocus="dchangemin()"></asp:TextBox>至
<asp:TextBox ID="txtACTIVITYENDTIME" runat="server" Width="100px" onfocus="dchangemax()"></asp:TextBox>
<input id="hid_biao" type="text" value="D" />
<input type="text" id="txt_jiage" />
<script src="http://yeebee.com.cn/js2/jquery.main.js"></script>
<script>
function dchangemin() {
var strBiao = $("#hid_biao").val();
switch (strBiao) {
case "M":
WdatePicker({ dateFmt: ‘yyyy-MM-dd‘, readOnly: true, minDate: ‘%y-%M-{%d+1‘,maxDate:‘#F{$dp.$D(\‘<%=txtACTIVITYENDTIME.ClientID%>\‘)}‘ });
break ;
case "D":
WdatePicker({ dateFmt: ‘yyyy-MM-dd‘, readOnly: true, minDate: ‘%y-%M-{%d+1‘, maxDate: ‘#F{$dp.$D(\‘<%=txtACTIVITYENDTIME.ClientID%>\‘)}‘ });
break;
case "H":
WdatePicker({ dateFmt: ‘yyyy-MM-dd HH‘, readOnly: true, minDate: ‘%y-%M-{%d+1‘, maxDate: ‘#F{$dp.$D(\‘<%=txtACTIVITYENDTIME.ClientID%>\‘)}‘ });
break;
default:
WdatePicker({ dateFmt: ‘yyyy-MM-dd‘, readOnly: true, minDate: ‘%y-%M-{%d+1‘, maxDate: ‘#F{$dp.$D(\‘<%=txtACTIVITYENDTIME.ClientID%>\‘)}‘ });
break;
}
$.ajax({
type: "Post",
url: "Add.aspx/strDiff",
//方法传参的写法一定要对,str为形参的名字,str2为第二个形参的名字
data: "{‘starttime‘:‘"+$(‘#<%= txtACTIVITYTIME.ClientID%>‘).val()+"‘,‘endtime‘:‘"+$(‘#<%= txtACTIVITYENDTIME.ClientID%>‘).val()+"‘,‘strBiao‘:‘"+strBiao+"‘}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
//返回的数据用data.d获取内容
$("#txt_jiage").val(data.d);
},
error: function(err) {
}
})
}
function dchangemax() {
var strBiao = $("#hid_biao").val();
switch (strBiao) {
case "M":
WdatePicker({ dateFmt: ‘yyyy-MM-dd‘, readOnly: true, minDate: ‘%y-%M-{%d+1‘, minDate: ‘#F{$dp.$D(\‘<%=txtACTIVITYTIME.ClientID%>\‘)}‘ });
break;
case "D":
WdatePicker({ dateFmt: ‘yyyy-MM-dd‘, readOnly: true, minDate: ‘%y-%M-{%d+1‘, minDate: ‘#F{$dp.$D(\‘<%=txtACTIVITYTIME.ClientID%>\‘)}‘ });
break;
case "H":
WdatePicker({ dateFmt: ‘yyyy-MM-dd HH‘, readOnly: true, minDate: ‘%y-%M-{%d+1‘, minDate: ‘#F{$dp.$D(\‘<%=txtACTIVITYTIME.ClientID%>\‘)}‘ });
break;
default:
WdatePicker({ dateFmt: ‘yyyy-MM-dd‘, readOnly: true, minDate: ‘%y-%M-{%d+1‘, minDate: ‘#F{$dp.$D(\‘<%=txtACTIVITYTIME.ClientID%>\‘)}‘ });
break;
}
$.ajax({
type: "Post",
url: "Add.aspx/strDiff",
//方法传参的写法一定要对,str为形参的名字,str2为第二个形参的名字
data: "{‘starttime‘:‘" + $(‘#<%= txtACTIVITYTIME.ClientID%>‘).val() + "‘,‘endtime‘:‘" + $(‘#<%= txtACTIVITYENDTIME.ClientID%>‘).val() + "‘,‘strBiao‘:‘" + strBiao + "‘}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
//返回的数据用data.d获取内容
$("#txt_jiage").val(data.d);
},
error: function (err) {
}
})
}
</script>
</td>
后台
[WebMethod]
public static string strDiff(string starttime, string endtime, string strBiao)
{
string strCount = "0";
try
{
if (strBiao == "H")
{
starttime += ":00";
endtime += ":00";
}
DateTime start = Convert.ToDateTime(starttime);
DateTime end = Convert.ToDateTime(endtime);
TimeSpan ts = end - start;
switch (strBiao)
{
case "M":
strCount = Math.Floor((ts.TotalDays / 31 + (ts.TotalDays % 31 > 0 ? 1 : 0))).ToString(); ;
break;
case "D":
strCount = ts.TotalDays.ToString();
break;
case "H":
strCount = ts.TotalHours.ToString();
break;
default:
strCount = ts.TotalDays.ToString();
break;
}
}
catch (Exception)
{
strCount = "0";
}
return strCount;
}