【问题标题】:How to determine if the date pick/choose from datepicker is a holiday?如何确定从 datepicker 中选择/选择的日期是否是假期?
【发布时间】:2016-12-29 17:08:23
【问题描述】:

我正在开发一个 asp.net mvc3 项目。我在下面有这张照片,日期为8/29/2016,一定是假期。而且我在我的 SQL Server 2k8 中有一个假期表,所以我的 sql 表中的日期是假期。我想要的是,如果它是假期,天数必须-(minus) the # of holiday,具体取决于申请期间有多少假期。

我在下面有这个代码用于我的开始休假

$("#LSDate").change(function (event) {
    var start = $("#LSDate").val();
    var end = $("#LEDate").val();
    var EmployeeId = $("#hidEmployeeId").val();
    var LeaveTypeId = $("#LeaveType").val();
    saveHalfDay = false;
    saveHalfDayPM = false;
    $("#LeaveIsHalfDay").attr("checked", false);
    if (LeaveTypeId == 3) {
        $.ajax({
            type: "POST",
            url: '../Attendance/_NoDaysApplied',
            data: '&EmployeeId=' + EmployeeId + '&StartDate=' + start + '&LeaveTypeId=' + LeaveTypeId,
            dataType: 'json',
            success: function (response) {
                if (response != null) {
                    $("#LEDate").val(response.msg);
                    $("#txtNoDays").val(response.noofdays);
                    $("#txtNoHrs").val(response.noofdays * 8);
                }
                else {
                    alert("Saving failed. Date Applied exists!");
                    $("#LSDate").val("MM/dd/yyyy");
                    $("#LEDate").val("MM/dd/yyyy");
                    $("#txtNoDays").val(0);
                    $("#txtNoHrs").val(0);
                }
            },
            error: function (reponse) { }
        });
    }
    else if (end != "MM/dd/yyyy" && end != "") {
        $.ajax({
            type: "POST",
            url: '../Attendance/_NoDaysLeaveApplied',
            data: '&EmployeeId=' + EmployeeId + '&StartDate=' + start + '&EndDate=' + end + '&LeaveTypeId=' + LeaveTypeId,
            dataType: 'json',
            success: function (response) {
                if (response != null) {
                    //alert("sucess" + response);
                    var dayoff = response.dayoff;
                    var a = $("#LSDate").datepicker('getDate').getTime(),
                    b = $("#LEDate").datepicker('getDate').getTime(),
                    c = 24 * 60 * 60 * 1000,
                    diffDays = Math.round(Math.abs((a - b) / (c)));
                    var days = (diffDays) + 1;
                    var numdays = (days - dayoff);
                    if (a > b) {
                        numdays = 0;
                        alert("End of Leave must be greater than start of leave.");
                    }
                    var hrs = numdays * 8;
                    $("#txtNoDays").val(numdays);
                    $("#txtNoHrs").val(hrs);
                    if ($("#LeaveType").val() == 16) {
                        var url1 = '../BenefitManagement/_SELValidation';
                        var _data1 = '&StartDate=' + $("#LSDate").val() + '&EndDate=' + $("#LEDate").val();
                        SELValidation(url1, _data1)
                    }
                }
                else {
                    alert("Please contact the administrator."); //alert("Saving failed. Date Applied exists!");
                    $("#LSDate").val("MM/dd/yyyy");
                    $("#LEDate").val("MM/dd/yyyy");
                    $("#txtNoDays").val(0);
                    $("#txtNoHrs").val(0);
                }
            },
            error: function (reponse) { }
        });
    }
});

启动离开控制器

[HttpPost]
    public ActionResult _NoDaysApplied(string EmployeeId, string StartDate, int LeaveTypeId)
    {
        int empId = Convert.ToInt32(EmployeeId);
        DateTime Sdate = Convert.ToDateTime(StartDate);
        DateTime Edate = Sdate;
        var response = new JsonResult();
        var LeavePolicy = (from a in db.LeavePolicies where a.LeaveTypeId == LeaveTypeId select a).SingleOrDefault();
        var Employee = (from a in db.Employees where a.EmployeeId == empId select a).SingleOrDefault();
        if (LeavePolicy.IsIncludeRestDay == true)
        {
            if (Employee.OriginalAppointment != null)
            {
                if (LeaveTypeId == 3 && Employee.OriginalAppointment.Value.AddDays(720) <= DateTime.Now)
                {
                    Edate = Sdate.AddDays(59);
                    var strEdate = Edate.ToString("MM/dd/yyyy");
                    int IsExist = (from ld in db.LeaveDetails where ld.TranDate >= Sdate && ld.TranDate <= Edate && ld.TranType == "L" && ld.EmployeeId == empId && ld.IsDisApproved == false && ld.TotalHours > 0 select ld.TranDate).Count();
                    if (IsExist == 0)
                    {
                        response.Data = new
                        {
                            msg = strEdate,
                            noofdays = 60
                        };
                    }
                    else
                    {
                        response.Data = new
                        {
                            msg = "Leave exists."
                        };
                    }
                }
                else if (LeaveTypeId == 3 && Employee.OriginalAppointment.Value.AddDays(270) >= DateTime.Now)
                {
                    TimeSpan diff = DateTime.Now - Employee.OriginalAppointment.Value;
                    int noofdays = diff.Days / 12;
                    Edate = Sdate.AddDays(noofdays - 1);
                    var strEdate = Edate.ToString("MM/dd/yyyy");
                    int IsExist = (from ld in db.LeaveDetails where ld.TranDate >= Sdate && ld.TranDate <= Edate && ld.TranType == "L" && ld.EmployeeId == empId && ld.IsDisApproved == false && ld.TotalHours > 0 select ld.TranDate).Count();
                    if (IsExist == 0)
                    {
                        response.Data = new
                        {
                            msg = strEdate,
                            noofdays = noofdays
                        };
                    }
                    else
                    {
                        response.Data = new
                        {
                            msg = "Leave exists."
                        };

                    }
                }
            }
            else
            {
                response.Data = new
                {
                    msg = "Original Appointment is required."
                };
            }
        }
        return response;
    }

结束休假日期选择器

$("#LEDate").change(function (event) {
    var end = $("#LEDate").val();
    var start = $("#LSDate").val();
    var EmployeeId = $("#hidEmployeeId").val();
    var LeaveTypeId = $("#LeaveType").val();
    $("#LeaveIsHalfDay").attr("checked", false);
    saveHalfDay = false;
    saveHalfDayPM = false;
    if (start == "MM/dd/yyyy") {
        alert("Please fill in Start Date!");
    }
    else {
        $.ajax({
            type: "POST",
            url: '../Attendance/_NoDaysLeaveApplied',
            data: '&EmployeeId=' + EmployeeId + '&StartDate=' + start + '&EndDate=' + end + '&LeaveTypeId=' + LeaveTypeId,
            dataType: 'json',
            success: function (response) {
                if (response != null) {
                    //alert("sucess" + response);
                    var dayoff = response.dayoff;
                    var a = $("#LSDate").datepicker('getDate').getTime(),
                    b = $("#LEDate").datepicker('getDate').getTime(),
                    c = 24 * 60 * 60 * 1000,
                    diffDays = Math.round(Math.abs((a - b) / (c)));
                    var days = (diffDays) + 1;
                    var numdays = (days - dayoff);
                    if (a > b) {
                        numdays = 0;
                        alert("End of Leave must be greater than start of leave.");
                    }
                    var hrs = numdays * 8;
                    $("#txtNoDays").val(numdays);
                    $("#txtNoHrs").val(hrs);

                    if ($("#LeaveType").val() == 16) {
                        var url1 = '../BenefitManagement/_SELValidation';
                        var _data1 = '&StartDate=' + $("#LSDate").val() + '&EndDate=' + $("#LEDate").val();
                        SELValidation(url1, _data1)
                    }
                }
                else {
                    alert("Please contact the administrator.");
                    $("#LSDate").val("MM/dd/yyyy");
                    $("#LEDate").val("MM/dd/yyyy");
                    $("#txtNoDays").val(0);
                    $("#txtNoHrs").val(0);
                }
            },
            error: function (reponse) { }
        });
    }
});

结束离开控制器

 [HttpPost]
    public ActionResult _NoDaysLeaveApplied(string EmployeeId, string StartDate, string EndDate, int LeaveTypeId)
    {
        int empId = Convert.ToInt32(EmployeeId);
        DateTime Sdate = Convert.ToDateTime(StartDate);
        DateTime Edate = Convert.ToDateTime(EndDate);
        var res = new JsonResult();
        int IsExist = (from ld in db.LeaveDetails where ld.TranDate >= Sdate && ld.TranDate <= Edate && ld.TranType == "L" && ld.EmployeeId == empId && ld.IsDisApproved == false && ld.TotalHours > 0 select ld.TranDate).Count();
        var LeavePolicy = (from a in db.LeavePolicies orderby a.LeavePolicyId where a.LeaveTypeId == LeaveTypeId select a).FirstOrDefault();
        if (LeavePolicy.IsIncludeRestDay == false)
        {
            int checkshift = (from sd in db.ShiftDetails where sd.EmployeeId == empId && sd.DateApplied >= (Sdate) && sd.DateApplied <= (Edate) select sd.EmployeeId).Count();
            int dayoff = 0;
            if (IsExist == 0)
            {
                if (checkshift > 0)
                {
                    int isDayoff = (from sd in db.ShiftDetails where sd.EmployeeId == empId && sd.DateApplied >= (Sdate) && sd.DateApplied <= (Edate) && (sd.IsDayOff == true) select sd.EmployeeId).Count();
                    int isHoliday = (from sd in db.ShiftDetails where sd.EmployeeId == empId && sd.DateApplied >= (Sdate) && sd.DateApplied <= (Edate) && (sd.IsHoliday == true) select sd.EmployeeId).Count();
                    dayoff = (isDayoff + isHoliday);
                }
                else
                {
                    TimeSpan diff = Edate - Sdate;
                    int days = diff.Days;
                    for (var i = 0; i <= days; i++)
                    {
                        var testDate = Sdate.AddDays(i);
                        switch (testDate.DayOfWeek)
                        {
                            case DayOfWeek.Saturday: dayoff++;
                                break;
                            case DayOfWeek.Sunday: dayoff++;
                                break;
                        }
                    }
                }
            }
            else
            {
                return null;
            }

            res.Data = new
            {
                dayoff
            };
        }
        else
        {
            res.Data = new
            {
                dayoff = 0
            };
        }
        return res;
    }

【问题讨论】:

    标签: javascript asp.net asp.net-mvc-3


    【解决方案1】:

    我现在解决了,谢谢。我在js和控制器中创建了一个函数就是它

    var txtdy, countholiday;
    var nhrs;
    function getHolidayCount(EmployeeId, start, end) {
        var isCount = 0;
        var nofhrs = 8;
        var url = '../Attendance/_getHolidayCount';
        var txtday = $("#txtNoDays").val();
        var data = '&EmployeeId=' + EmployeeId + '&StartDate=' + start + '&EndDate=' + end;
        $.ajax({
            type: "POST",
            url: url,
            data: data,
            dataType: 'json',
            async: false,
            success: function (response) {
                isCount = response.isCount;
                countholiday = isCount;
                if (isCount > 0) {
                    txtday = txtday - isCount;
                    txtdy = txtday;
                    nhrs = nofhrs * txtday;
                }
            },
            error: function (reponse) { }
        });
        return isCount;
    }
    

    对于控制器

    [HttpPost]
        public ActionResult _getHolidayCount(string EmpId, string StartDate, string EndDate)
        {
            //Boolean isTrue = false;
            var isCount = "";
    
            int empId = Convert.ToInt32(EmpId);
            DateTime Sdate = Convert.ToDateTime(StartDate);
            DateTime Edate = Convert.ToDateTime(EndDate);
    
            while (Sdate <= Edate)
            {
                if (!(Sdate.DayOfWeek == DayOfWeek.Saturday || Sdate.DayOfWeek == DayOfWeek.Sunday))
                {
                    var getHolidayCount = (from a in db.Holidays where a.HolidayDate == Sdate.Date select a.HolidayDate).Count();
    
    
                    if (getHolidayCount > 0)
                    {
                        isCount = Convert.ToString(getHolidayCount);
                    }
                }
                Sdate = Sdate.AddDays(1);
            }
            var res = new JsonResult();
            res.Data = new
            {
                isCount = isCount
            };
            return res;
        }
    

    我希望它也能帮助其他人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-14
      • 1970-01-01
      • 1970-01-01
      • 2012-12-20
      • 1970-01-01
      • 1970-01-01
      • 2013-11-08
      • 1970-01-01
      相关资源
      最近更新 更多