1.工作日设置
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Cannder.aspx.cs" Inherits="SqliteDemo.Demo.Cannder" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>工作日设置</title> <script src="/Scripts/jquery-1.10.2.min.js"></script> <script src="/Scripts/string.js"></script> <style type="text/css"> #current_month_info { height: 26px; line-height: 26px; margin-bottom: 10px; font-size: 14px; font-weight: 700; color: #5f5f5f; } #current_month_info_o { padding-left: 30px; } .edit-group { margin-top: 20px; } .txt-edit { text-indent: 5px; 150px; font-weight: 300; } .pnl_date_setting { position: relative; } table.grid { border-collapse: collapse; border: 1px solid #d3d3d3; } table.grid th, table.grid td { border: 1px solid #d3d3d3; border-left: 0; border-right: 0; text-align: center; 80px; padding: 0; } table.grid th { height: 30px; line-height: 30px; } table.grid td { height: 80px; line-height: 80px; cursor: pointer; font-family: arial; font-size: 18px; position: relative; } td a:hover { 76px; height: 76px; border: 2px solid #fb0; text-decoration: none; line-height: 76px; } td span { position: absolute; left: 0; top: 0; 18px; height: 18px; line-height: 18px; text-align: center; font-size: 12px; display: block; } a.everyday { float: left; 100%; background-color: #fff; height: 80px; } a.hld-day { color: #e02d2d; } a.not-current-month { color: #bfbfbf; } a.happy-day { background-color: #fff0f0; } a.happy-day span { background-color: #f43; color: #fff; } a.work-day { background-color: #f5f5f5; } a.work-day span { background-color: #969799; color: #fff; } a.current-day { background-color: #fb0; color: #fff; } ul, li { list-style-type: none; } .date_setting_win { border: 2px solid #fb0; position: absolute; z-index: 2; background-color: #fff; top: 32px; } .date_setting_win ul { 78px; } .date_setting_win li { height: 26px; } .date_setting_win a { float: left; line-height: 26px; 100%; cursor: pointer; text-align: center; } .set-sbr a { background-color: #f5f5f5; } .set-xxr a { background-color: #fff0f0; } body, div, li, ul { margin: 0; padding: 0; } .usc-container { 50%; margin: 0px auto; } </style> </head> <body> <div class="usc-container"> <div class="panl-grid " style="min-height: 547px;"> <div id="current_month_info">今天是:<%=string.Format("{0:yyyy年MM月dd日}", DateTime.Now) %> <span id="current_month_info_o">当前月:<%=string.Format("{0:yyyy年MM月}", DateTime.Now) %></span></div> <!-- 1900年1月1号 星期一 --> <div class="pnl_date_setting"> <div id="date_setting_win" class="date_setting_win" style="display: none;"> <ul> <li class="set-bsz"><a onclick="SetDayStat(0)">不设置</a></li> <li class="set-sbr"><a onclick="SetDayStat(2)">工作日</a></li> <li class="set-xxr"><a onclick="SetDayStat(3)">休息日</a></li> </ul> </div> <table id="grid" class="grid"> <thead> <tr> <th>星期一</th> <th>星期二</th> <th>星期三</th> <th>星期四</th> <th>星期五</th> <th style="color: #e02d2d;">星期六</th> <th style="color: #e02d2d;">星期天</th> </tr> </thead> <tbody> </tbody> </table> </div> </div> </div> <script type="text/javascript"> String.prototype.toDate = function () { if(this==null) throw new TypeError('String.prototype.toDate called on null or undefined'); return new Date(this.split("-")[0],parseInt(this.split("-")[1])-1,this.split("-")[2]) } var currentDate = '<%=string.Format("{0:yyyy-MM-dd}", DateTime.Now) %>'.toDate(); var currentDay = '<%=string.Format("{0:yyyy-MM-dd}", DateTime.Now) %>'.toDate(); var currentYear = <%=DateTime.Now.Year %>; var currentAllhld = <%=FindCurrentYeayAllHoliday(DateTime.Now.Year) %>; var list={ "01-01": "元旦", "02-14": "情人", "03-08": "妇女", "03-12": "植树", "04-01": "愚人", "05-01": "劳动", "05-04": "青年", "06-01": "儿童", "09-10": "教师", "09-18": "国耻", "10-1": "国庆", "12-25": "圣诞" }; function ShowDiv(that) { $(that).parent().toggleClass("extend"); } function CreateNewRow(that) { var thatRow = $(that).parents("tr:eq(0)"); var thatGrd = thatRow.parent(); if (thatGrd.children(":last").index() == thatRow.index()) { thatGrd.children(":first") } } function getLastDayDate(dt) { dt.setDate(1); dt.setMonth(dt.getMonth() + 1); cdt = new Date(dt.getTime() - 1000 * 60 * 60 * 24); return cdt; } function SetDayStat(stat){ var currentObj = $('#grid td[strdate="{0}"] a'.format($("#date_setting_win").attr("setdate"))); currentObj.removeClass("happy-day").removeClass("work-day") $("#date_setting_win").hide(); if (stat == "2") { currentObj.addClass("work-day"); currentObj.find("span").html("班"); currentAllhld[DateToStringInZero($("#date_setting_win").attr("setdate").toDate(),"{0}_{1}_{2}")] = 2; } else if (stat == "3") { currentObj.addClass("happy-day"); currentObj.find("span").html("休"); currentAllhld[DateToStringInZero($("#date_setting_win").attr("setdate").toDate(),"{0}_{1}_{2}")] = 3; } else { currentObj.find("span").html(""); currentAllhld[DateToStringInZero($("#date_setting_win").attr("setdate").toDate(),"{0}_{1}_{2}")] = 0; } // $.ajax({ // type: "POST", // contentType: "application/json", // url: "/System/DateSettings.aspx/StringSetDayStat", // data: JSON.stringify({ strdate: $("#date_setting_win").attr("setdate"), strstat: stat }), // dataType: "json", // success: function (data) { // if (data.d == "ok") { // var currentObj = $('#grid td[strdate="{0}"] a'.format($("#date_setting_win").attr("setdate"))); // currentObj.removeClass("happy-day").removeClass("work-day") // if (stat == "2") { // currentObj.addClass("work-day"); // currentObj.find("span").html("班"); // currentAllhld[DateToStringInZero($("#date_setting_win").attr("setdate").toDate(),"{0}_{1}_{2}")] = 2; // } // else if (stat == "3") { // currentObj.addClass("happy-day"); // currentObj.find("span").html("休"); // currentAllhld[DateToStringInZero($("#date_setting_win").attr("setdate").toDate(),"{0}_{1}_{2}")] = 3; // } // else { // currentObj.find("span").html(""); // currentAllhld[DateToStringInZero($("#date_setting_win").attr("setdate").toDate(),"{0}_{1}_{2}")] = 0; // } // return; // } // js.alert(data.d); // }, // error: function (e) { // js.alert("失败"); // js.screen(false); // } // }); } function GetNewDate(dt) { return "{0}-{1}-{2}".format(dt.getFullYear(), dt.getMonth() + 1, dt.getDate()).toDate(); } function DateToString(dt) { return "{0}-{1}-{2}".format(dt.getFullYear(), dt.getMonth() + 1, dt.getDate()); } function DateToStringInZero(dt,strFormat) { var y = dt.getFullYear(); var m = dt.getMonth() + 1; var d = dt.getDate(); if(m<10) m = "0" + m; if(d<10) d = "0" + d; if(strFormat=="{0}_{1}") { return strFormat.format(m,d); } else if(strFormat) return strFormat.format(y, m,d); return "{0}-{1}-{2}".format(y, m,d); } function GetFirstWeek(dt) { var day = dt.getDate(); var week = dt.getUTCDay(); var firstWeek = week - (day % 7) + 1; return firstWeek; } function CreateDateTag(d) { $("#date_setting_win").hide(); $("#grid").attr("currentdate", DateToString(d)) $("#current_month_info_o").html("当前月:{0}年{1}月".format(d.getFullYear(), d.getMonth() + 1)); if(d.getFullYear()!=currentYear){ currentYear = d.getFullYear(); $.ajax({ type: "POST", contentType: "application/json", url: "/System/DateSettings.aspx/GetCurrentYeayAllHoliday", data: JSON.stringify({ year: currentYear }), dataType: "json", async:false, success: function (data) { if (data && data.d && data.d.length>0) { currentAllhld = JSON.parse(data.d); } else{ js.alert("获取本年度假日安排失败。"); } }, error: function (e) { js.alert("失败"); js.screen(false); } }); } var day = d.getDate(); var week = d.getUTCDay(); var firstWeek = DateToStringInZero(GetNewDate(d),"{0}-{1}-1").toDate().getUTCDay();// week - (day % 7) + 1; var strTagRows = []; var prevDT = GetNewDate(d); prevDT.setMonth(prevDT.getMonth() - 1); var datePrev = getLastDayDate(prevDT); //如果星期一时,第一排显示上月最后7天 var intPrevMonth = datePrev.getDate() - (firstWeek==0?7:firstWeek) + 1; var intPrevMonthLastDay = datePrev.getDate(); var intMonth = 1; var intMonthLastDay = getLastDayDate(GetNewDate(d)).getDate(); var nextDT = GetNewDate(d); nextDT.setMonth(nextDT.getMonth() + 1); var intLast = 1; for (var i = 0; i < 6; i++) { strTagRows.push("<tr>"); for (var k = 0; k < 7; k++) { var currClassName = ""; var currStatName = ""; if (k > 4) currClassName = "hld-day " if (intPrevMonth <= intPrevMonthLastDay) { currClassName += "not-current-month "; var cStrFormat = DateToStringInZero("{0}-{1}-{2}".format(datePrev.getFullYear(),datePrev.getMonth() + 1,intPrevMonth).toDate(),"{0}_{1}_{2}"); if(currentAllhld[cStrFormat]){ if(currentAllhld[cStrFormat]==2){ currClassName+=" work-day "; currStatName = "班"; } else if(currentAllhld[cStrFormat]==3){ currClassName+=" happy-day "; currStatName = "休"; } } strTagRows.push("<td strdate="{0}-{1}-{2}"><a class="everyday {3}"><span>{4}</span>".format(datePrev.getFullYear(), datePrev.getMonth() + 1, intPrevMonth, currClassName,currStatName)); strTagRows.push(intPrevMonth); strTagRows.push("</a></td>"); intPrevMonth++; continue; } else if (intMonthLastDay < intMonth) { currClassName += "not-current-month"; var cStrFormat = DateToStringInZero("{0}-{1}-{2}".format(nextDT.getFullYear(), nextDT.getMonth() + 1, intLast).toDate(),"{0}_{1}_{2}"); if(currentAllhld[cStrFormat]){ if(currentAllhld[cStrFormat]==2){ currClassName+=" work-day "; currStatName = "班"; } else if(currentAllhld[cStrFormat]==3){ currClassName+=" happy-day "; currStatName = "休"; } } strTagRows.push("<td strdate="{0}-{1}-{2}"><a class="everyday {3}"><span>{4}</span>".format(nextDT.getFullYear(), nextDT.getMonth() + 1, intLast, currClassName,currStatName)); strTagRows.push(intLast); strTagRows.push("</a></td>"); intLast++; } else { currClassName += (day == intMonth ? "current-day" : ""); var cStrFormat = DateToStringInZero("{0}-{1}-{2}".format(d.getFullYear(), d.getMonth() + 1, intMonth).toDate(),"{0}_{1}_{2}"); if(currentAllhld[cStrFormat]){ if(currentAllhld[cStrFormat]==2){ currClassName+=" work-day "; currStatName = "班"; } else if(currentAllhld[cStrFormat]==3){ currClassName+=" happy-day "; currStatName = "休"; } } strTagRows.push("<td strdate="{0}-{1}-{2}"><a class="everyday {3}"><span>{4}</span>".format(d.getFullYear(), d.getMonth() + 1, intMonth, currClassName,currStatName)); strTagRows.push(intMonth); strTagRows.push("</a></td>"); } var jr = DateToStringInZero("{0}-{1}-{2}".format(datePrev.getFullYear(),datePrev.getMonth() + 1,intPrevMonth).toDate(),"{0}_{1}"); alert(list[jr]); if(list[jr]){ currClassName+=" happy-day "; currStatName = list[jr]; } intMonth++; } strTagRows.push("</tr>"); } $("#grid tbody").html(""); $("#grid tbody").append(strTagRows.join("")); } //生成今天 CreateDateTag(GetNewDate(currentDay)); function CreateOtherMonthDateTag(m) { var dt = $("#grid").attr("currentdate").toDate(); dt.setMonth(dt.getMonth() + m); CreateDateTag(dt); } function CreateOtherYearDateTag(y) { var dt = $("#grid").attr("currentdate").toDate(); dt.setYear(dt.getFullYear() + y); CreateDateTag(dt); } $(function () { $("#grid").click(function (e) { var that = e.target; if (that.tagName == "A" || that.tagName == "SPAN") { var thatCol = $(that).parents("td:eq(0)"); // if($(thatCol).find("A")[0].className.indexOf("not-current-month")>-1){ // var at = $("#grid").attr("currentdate").toDate() // if(thatCol.attr("strdate").toString().toDate() < at) { // at.setMonth(at.getMonth() - 1 ); // } // else{ // at.setMonth(at.getMonth() + 1 ); // } // CreateDateTag(at); // thatCol = $('#grid td[strdate="{0}"]'.format(thatCol.attr("strdate"))) // return; // } var colIndex = thatCol[0].cellIndex; var rowIndex = thatCol.parent()[0].rowIndex; $("#date_setting_win").show() .animate({ top: (((rowIndex - 1) * 80) + 30 + rowIndex), left: (colIndex * 80) }, "fast") .attr("setdate", thatCol.attr("strdate")) ; } }); }) </script> </body> </html>
public partial class Cannder : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } [WebMethod] public static string GetCurrentYeayAllHoliday(string year) { return FindCurrentYeayAllHoliday(Convert.ToInt32(year)); } protected static string FindCurrentYeayAllHoliday(int year) { //JjrszbServiceBase serviceBase = new JjrszbServiceBase(); int count; //var list = serviceBase.Find(string.Format("JJRSJ > to_date('{0}-11-30 23:59:59','yyyy-mm-dd hh24:mi:ss') AND JJRSJ < to_date('{1}-04-01 00:00:00','yyyy-mm-dd hh24:mi:ss')", year - 1, year + 1), "JJRSJ", out count); var dateSb = new StringBuilder(); dateSb.Append("{"); //if (list != null && list.Count > 0) //{ // foreach (var item in list) // { // if (dateSb.Length > 1) // dateSb.Append(","); // dateSb.AppendFormat(""{0:yyyy_MM_dd}":"{1}"", item.JJRSJ, item.JJRLB); // } //} dateSb.Append("}"); return dateSb.ToString(); //return ""; } }
/* * Start Date : 2012-4-30 00:29:45 * Update Date : 2012-4-30 00:29:45 * Update Date : 2012-5-2 17:37:50 * Comment : 对扩展函数处理 throw new TypeError() 的使用 * Author : qingbo.guo * * 作用 : 字符串扩展类 */ /* * 格式化字符串 */ if (!String.prototype.format) String.prototype.format = function () { var args = arguments && arguments[0] instanceof Array ? arguments[0] : arguments; return this.replace(/{(d+)}/g, function (_search, _replace, _index, _string) { //_search 搜索的结果 要被替换的字符串 //_replace 将被替换成的字符串 //_index 搜索结果 要被替换的字符串索引 //_string 被操作的原字符串 return args[_replace]; }); }; /* * 去除所有空格 * String.prototype.trimAll = function () { * return this.replace(/(^s*)|(s*)|(s*$)/g, ""); * }; */ if (!String.prototype.trimAll) String.prototype.trimAll = function () { if (this == null) //throw new TypeError() 抛出一个异常 throw new TypeError('String.prototype.trimAll called on null or undefined'); return String.prototype.replace.call(this, /(^s+)|(s+)|(s+$)/g, ''); }; /* * 去两边空格 * String.prototype.trimAll = function () { * return this.replace(/(^s*)|(s*$)/g, ""); * }; */ if (!String.prototype.trim) String.prototype.trim = function () { if (this == null) //throw new TypeError() 抛出异常 throw new TypeError('String.prototype.trimAll called on null or undefined'); return this.replace(/(^s*)|(s*$)/g, ""); };
#region 实体 /// <summary> /// 表名 /// </summary> public string TableName { get { return "JJRSZB"; } } /// <summary> /// 主键名 /// </summary> public string PrimaryKey { get { return "JJRSZID"; } } /// _summary> /// /// _/summary> public Double JJRSZID { get; set; } /// _summary> /// /// _/summary> public DateTime JJRSJ { get; set; } /// _summary> /// /// _/summary> public Double JJRLB { get; set; } #endregion
2.获取工作日
/// <summary> /// 计算从开始时间到x天之后出去休息日要多少天 /// </summary> /// <param name="NowData">计算开始时间</param> /// <param name="IntervalDay">多少天之后</param> /// <returns></returns> public int IntervalNoJobDays(DateTime NowData, int IntervalDay) { int outCount; int countday =0; if (IntervalDay <= 0) return 0; for (int i = 1; i >0;i++ ) { NowData = NowData.AddDays(1); List<JjrszbBase> list = Find(" to_char(jjrsj,'yyyy-MM-dd') = '"+NowData.ToString("yyyy-MM-dd")+"'","",out outCount); if (outCount > 0) { if (list[0].JJRLB == 2) { countday++; } } else { int weekday =(int)NowData.DayOfWeek; if (!(weekday==0||weekday==6)) countday++; } if (countday>= IntervalDay) return i; } return countday; } /// <summary> /// 计算两时间段的工作日 /// </summary> /// <param name="beginDateTime">开始时间</param> /// <param name="endDateTime">结束时间</param> /// <returns></returns> public int JobDayCount(DateTime beginDateTime, DateTime endDateTime) { TimeSpan ts1 = new TimeSpan(beginDateTime.Ticks); TimeSpan ts2 = new TimeSpan(endDateTime.Ticks); TimeSpan ts = ts1.Subtract(ts2).Duration(); int days = ts.Days+1; int outCount; List<JjrszbBase> list = Find("jjrsj>=to_date('" + beginDateTime.ToString("yyyy-MM-dd") + "','yyyy-MM-dd') and jjrsj<=to_date('" + endDateTime.ToString("yyyy-MM-dd") + "','yyyy-MM-dd')", "", out outCount); for (int i = 1; i <= ts.Days; i++) { DateTime time = beginDateTime.AddDays(i); JjrszbBase listJrisz = list.Find(o => o.JJRSJ.ToString("yyyy/MM/dd").Equals(time.ToString("yyyy/MM/dd"))); if (listJrisz != null && listJrisz.JJRSZID > 0 && listJrisz.JJRLB == 3) { days--; } else { int weekday =(int)time.DayOfWeek; if ((weekday == 0 || weekday == 6)) days--; } } return days; }
/// <summary> /// 获取工作日天数 /// </summary> /// <param name="startDate"></param> /// <param name="endDate"></param> /// <returns></returns> public static int GetWorkingdaysNum(DateTime startDate, DateTime endDate) { //取两个时间的Date startDate = startDate.Date; endDate = endDate.Date; int workingdays = 0; //获取两个日期之间的特殊时间 JjrszbServiceBase serviceBase = new JjrszbServiceBase(); //List<JjrszbBase> list = serviceBase.Find("", "", out count); //获取两个时间内的时间设置 var list = from special in serviceBase.GetAll() where special.JJRSJ >= startDate && special.JJRSJ <= endDate select special; while (startDate <= endDate) { var day = startDate.DayOfWeek.ToString(); //表示是周末 if (startDate.DayOfWeek == DayOfWeek.Sunday || startDate.DayOfWeek == DayOfWeek.Saturday) { //查询周末是否上班 if (list.Count(p => p.JJRSJ == startDate && p.JJRLB == 2) > 0) workingdays++; } else { //查询工作日是否休息 if (list.Count(p => p.JJRSJ == startDate && p.JJRLB == 3) == 0) workingdays++; } //判断是否是周末 startDate = startDate.AddDays(1); } return workingdays; }
读取工作日休息日未全自动实现。
设置工作日,休息日没有实现保存到数据库,请自行修改保存相关的内容