使用jquery获取Startdate和enddate之间的日期并在表格中显示

时间:2016-04-27 07:34:17

标签: jquery

就像我有两个输入框txtdatefrom和txtdateUpto

txtdatefrom = 2016年4月1日

txtDateUpto = 2016年4月30日

知道表格中的show输出 2016年3月30日

2016年4月29日

2016年4月28日

2016年4月27日

.......

.....等等

2016年4月1日

1 个答案:

答案 0 :(得分:-1)

 <script type="text/javascript">  

  function getMonthNumber(Name) {
        var month = new Array();
        month["Jan"] = 0;
        month["Feb"] = 1;
        month["Mar"] = 2;
        month["Apr"] = 3;
        month["May"] = 4;
        month["Jun"] = 5;
        month["Jul"] = 6;
        month["Aug"] = 7;
        month["Sep"] = 8;
        month["Oct"] = 9;
        month["Nov"] =10 ;
        month["Dec"] = 11;
        return month[Name];
    }
    function CreateDateRange() {
        var dateFromText = $("[Id*=txtDateFrom]").val();
        var dateUptoText = $("[Id*=txtDateUpto]").val();
        if (dateFromText == null || dateFromText == "") {
            alert("Date From Required");
            return;
        }
        if (dateUptoText == null || dateUptoText == "") {
            alert("Date Upto Required");
            return;
        }

        var dateFromArr = dateFromText.split("-");
        var dateUptoArr = dateUptoText.split("-");            
        var dateFrom = new Date(dateFromArr[2], getMonthNumber(dateFromArr[1]), dateFromArr[0]);
        var dateUpto = new Date(dateUptoArr[2], getMonthNumber(dateUptoArr[1]), dateUptoArr[0]);
        var a = 1;
        while (dateFrom <= dateUpto) {               
            $("#tblDRDateWise").append('<tr><td class="centerAligned">  ' + a++ + ' </td><td class="centerAligned"><input type="image" src="../images/plus.gif" id="btnDateWise" onclick="return getdataDateWise(this)"/></td><td class="leftAligned" ><input type="hidden" name="hidden" id="hdn" value=' + dateUpto + ' /></td><td>' + dateUpto + '</td></tr>');
            dateUpto = new Date(dateUpto.setDate(dateUpto.getDate() - 1))

        }           
    }


    function getdataDateWise(obj) {
        row = $(obj).closest("tr");            
        var dateUpto = $("[Id*=hdn]", row).val();
        if ($(obj).attr("src") == '../images/plus.gif') {
            $(obj).attr('src', '../images/minus.gif');                               
            var Zone = $("[Id*=ddlZone]").val();
            var ActivityBy = $("[Id*=ddlUser]").val();
            var ActivityType = $("[Id*=ddlActivityType]").val();                

            $.ajax({
                type: "POST",
                url: "DailyReportDateWise.aspx/DailyReportDateWise",
                data: "{'dateUpto':'" + dateUpto + "','Zone':'" + Zone + "','ActivityBy':'" + ActivityBy + "','ActivityType':'" + ActivityType + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data)
                {
                    var rowdata = "<tr id='trprimaryPro'> <td></td><td colspan='4'>";
                    rowdata += "<table id='tblDateWiseTwo' class='GridDefault' width='90%' cellspacing='0' cellpadding='4' border='1' style='border-collapse: collapse;' rules='all'><thead><tr class='GridHeaderStyle' style='background-color:#4da6ff;'><td style='width:5%;text-align:center'>SNo.</td><td style='width: 25% ;text-align:left'>Name</td><td style='width: 25% ;text-align:left'>Remark</td><td style='width: 20% ;text-align:center'>Activity Name</td></tr></thead><tbody>";
                    if (data.d.length > 0) {
                        var a = 1;                            
                        for (var i = 0; i < data.d.length; i++) {
                            rowdata += "<tr ><td  class='centerAligned' >" + a++ + "</td><td class='leftAligned'>  " + data.d[i].Name + "</td><td class='leftAligned''>" + data.d[i].Remark + "</td><td class='centerAligned'>" + data.d[i].ActivityName + "</td></tr>";                                
                        }
                        //rowdata += "<tr><td style='width:3%;text-align:center'></td><td style='width:20%;text-align:left'></td><td style='width:15%;text-align:Center'>Total :</td><td style='width:5%;text-align:center'>" + TotalQty + "</td><td style='width:7%;text-align:right'>" + TotalAmount.toFixed(2) + "</td></tr>";
                        rowdata += "</tbody></table>";
                        rowdata += "</td>";
                        rowdata += "</tr>";
                        $(row).after(rowdata);
                    }
                    else {
                        rowdata += "<tr ><td colspan='6' style='text-align:center;font-size:large;color:red'>Sorry !! No data Found</td></tr></tr>";
                        $(row).after(rowdata);
                    }
                },
                error: function (result) {
                    $('#trprimaryPro tbody').remove();
                    $("#trprimaryPro").append("<tr><td colspan='6' style='text-align:center;font-size:large;color:red'>" + result + "</td></tr>");
                }
            });
        }
        else {
            $(obj).attr('src', '../images/plus.gif');
            $(row).next("#trprimaryPro").remove();
        }
        return false;
    }
</script>