My97DatePicker.js 之无效日期设置

时间:2022-02-06 03:17:58

1.下载并引用My97DatePicker.js

2.将工作日休假的和周末上班的维护到数据库中

3.取出某段时间内的不上班日期集合

/// <summary>
///获取 无效日期集合 1 工作日休息(放假)2 休息日上班(上班)
/// </summary>
/// <returns></returns>
public JsonResult GetValidate()
{
List<string> Lst = new List<string>();
List<DateTime> valiDateLst = new List<DateTime>(); //工作日休息
List<DateTime> unvaliDateLst = new List<DateTime>(); //周末上班

var dt = DateTime.Now;
//前一个月
var fromTime = new DateTime(dt.Year, dt.Month < 2 ? 1 : dt.Month-1, 1);
//后六个月
var toTime = dt.Month > 6 ? new DateTime(fromTime.Year+1,1,1).AddDays(-1): fromTime.AddMonths(7).AddDays(-1);
//TimeSpan得到fromTime和toTime的时间间隔
var ts = toTime.Subtract(fromTime);
//获取两个日期间的总天数
long countday = ts.Days;

//循环用来扣除总天数中的双休日
for (var i = 0; i < countday; i++)
{
var tempdt = fromTime.Date.AddDays(i);
if (tempdt.DayOfWeek == DayOfWeek.Saturday || tempdt.DayOfWeek== DayOfWeek.Sunday)
{
string str = tempdt.ToString("yyyy-MM-dd");
Lst.Add(str);
}
}

List<SpecialDays> specialDaysLst= _specialDaysService.GetAllSpecialDaysList();

if (specialDaysLst==null)
{
specialDaysLst = new List<SpecialDays>();
}
var valiDate = from SpecialDays a in specialDaysLst where a.DayType == 1select a;
var nuValidate = from SpecialDays a in specialDaysLst where a.DayType == 2 select a;
//工作日休息
foreach (var vali in valiDate)
{
if (vali.StartDate.HasValue && vali.EndDate.HasValue)
{
for (DateTime date = vali.StartDate.Value; date <= vali.EndDate; )
{
valiDateLst.Add(date);
date=date.AddDays(1);
}
}
}
//休息日上班
foreach (var vali in nuValidate)
{
if (vali.StartDate.HasValue && vali.EndDate.HasValue)
{
for (DateTime date = vali.StartDate.Value; date <= vali.EndDate;)
{
unvaliDateLst.Add(date);
date = date.AddDays(1);
}
}
}

for (int i = 0; i < valiDateLst.Count; i++)
{
if ((Convert.ToDateTime(valiDateLst[i]) > fromTime && Convert.ToDateTime(valiDateLst[i]) < toTime) && !Lst.Contains(valiDateLst[i].ToString("yyyy-MM-dd")))
{
Lst.Add(valiDateLst[i].ToString("yyyy-MM-dd"));
}
}
for (int i = 0; i < unvaliDateLst.Count; i++)
{
if (Convert.ToDateTime(unvaliDateLst[i]) > fromTime && Convert.ToDateTime(unvaliDateLst[i]) < toTime)
{
Lst.Remove(unvaliDateLst[i].ToString("yyyy-MM-dd"));
}
}
return Json(Lst, JsonRequestBehavior.AllowGet);
}

4.页面引用加过滤条件

$("#ipt_repaymentDate").on("focus", function (dp) {
WdatePicker({
//opposite: true,//如果不设置则disabledDates参数中的值会是“无效日期”
disabledDates: unvalidate_date_Array
});
});