如何确定从datepicker中选择日期是假日?

时间:2016-08-23 02:48:30

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

我正在开发一个asp.net mvc3项目。我在下面标注8/29/2016这张照片,这一定是假期。而且我的SQL Server 2k8中还有一个用于假期的表,以便日期从我的sql表开始。我想要的是,如果它是假日,则天数必须-(minus) the # of holiday,具体取决于提交期间的假期/ s。

PICTURE

我的开始假期下面有以下代码

$("#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;
    }

1 个答案:

答案 0 :(得分:0)

我现在解决了,谢谢。我在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;
    }

我希望它也会帮助别人。