方法一:Json方法 通过对查询出来的结果进行比较 使用函数CompareTo()
//Json方法 HttpContext.Current.Response.ContentType = "application/json"; DateTime[] times = BLL.Order.TimeBJManager.GetDateSchoolTime(); //times t1 = NowDT. (dt2); //NowDT 早于 dt2,返回 -1 //times t2 = NowDT.CompareTo(dt1); //NowDT 晚于 dt1,返回 1 //times t3 = NowDT.CompareTo(dt2); //NowDT == dt2,返回 0 if (NowDT.CompareTo(times[0]) < 0 || NowDT.CompareTo(times[1]) > 0) { //当前时间 早于开始时间 //当前时间 晚于结束时间 string json = "{"StartTime":"" + times[0] + "","EndTime": "" + times[1] + ""}"; HttpContext.Current.Response.Write(json); } else { HttpContext.Current.Response.Write(null); }
方法二:通过查询数据库,在数据库中比较时间 使用函数 CONVERT(data_type(length),data_to_be_converted,style)
DataTable dt=new DataTable(); /// <summary> /// 时间验证 /// </summary> /// <returns></returns> [WebMethod] public void Timeyanzheng() { StringBuilder strHtml = new StringBuilder(); DateTime NowDT = DateTime.Now; dt = new BLL.list.Lists().list("SYS_APPConfig", "Id,type,convert(varchar(100),StartDate,23) as StartDate,convert(varchar(100),EndDate,23) as EndDate", "").Tables[0]; // DataSet ds = new BLL.list.Lists().list("SYS_APPConfig", "convert(varchar(100),StartDate,23) as StartDate,convert(varchar(100),EndDate,23) as EndDate", " type='学校端设置' and StartDate<'" + NowDT + "' and EndDate>'" + NowDT + "'"); DataRow[] dr = dt.Select("type='学校端设置' and StartDate<'" + NowDT + "' and EndDate>'" + NowDT + "'"); if (dr != null && dr.Length > 0) { HttpContext.Current.Response.Write("suc"); } else { DataRow[] drControl = dt.Select(" type='学校端设置'"); strHtml.AppendFormat("<div><h3>学校预约时间为:{0} ~ {1}</h3></div>", drControl[0]["StartDate"].ToString(), drControl[0]["EndDate"].ToString()); HttpContext.Current.Response.Write(strHtml.ToString()); } }
以上是我自己用到的两种方法,至于后台的C#验证就不写了。