如何使用jquery获取结束时间

时间:2012-08-21 11:01:27

标签: javascript jquery

Demo这里在jsbin演示中我得到了开始时间,但我不知道如何获得结束时间。  我该如何进一步获得结束时间。 我不想改变HTML。 我必须使用现有的HTML获得结束时间。 我从最近三天开始尝试。

$("#btnAllot").click(function ()
            { var calculateStartMins = $('td.csstd:first');var calculateStartHrs = calculateStartMins.parent().prevAll().andSelf().find('td:first-child').filter(function(){return $.trim($(this).text()) !== '';}).last();
                    var fromTime = calculateStartHrs.text().trim() + calculateStartMins.text().trim();
                    var fromTimeAMPM = fromTime.substring(5, 7);
                    var fromTimeHour = fromTime.trim().substring(0, 2);
                    var fromTimeMinute = fromTime.substring(7, 10);
                    var hidFromTime = fromTimeHour.trim() +":"+ fromTimeMinute.trim();alert("Start Time is "+hidFromTime.trim()); 
             var endTime = $('td.csstd:last').parent().next().andSelf().find('td:first-child').
                filter(function ()
                {return $.trim($(this).text()) !== '';
               }).last();alert("End Time is "+endTime);
            });   

1 个答案:

答案 0 :(得分:1)

你的想法太复杂了,我想......看看这是否适合你:

$("#btnAllot").click(function(){
    var begin = $("td.csstd:first"), end = $("td.csstd:last"),
        beginHour, endHour, endMinute, currentHour, beginTime, endTime;
    $("tr.csstablelisttd").each(function(){
        var minuteTd = $(this).find("td").eq(1),
            t = $(this).find("td:first").text().trim();
        (t.length) && (currentHour = t);
        (minuteTd.filter(begin).length > 0) && (beginHour = currentHour);
        (minuteTd.filter(end).length > 0)   && (endHour   = currentHour);
    });

    endHour = endHour.replace(/00/, "").split(":");
    endHour[0] = Number(endHour[0]);
    endMinute = (Number(end.text().trim())+15) % 60;
    if (endMinute == 0) {
        endHour[0] = (endHour[0] + 1) % 12;
        if (endHour[0] == 0) {
            endHour[0] = 12;
            endHour[1] = endHour[1] == "AM" ? "PM" : "AM";
        }
    }
    (endHour[0] < 10) && (endHour[0] = "0"+endHour[0]);
    (endMinute < 10) && (endMinute = "0"+endMinute);

    beginTime = beginHour.replace(/00/, begin.text().trim()); 
    endTime   = endHour[0] + ":" + endMinute + endHour[1];

    alert("Start time is "+beginTime);
    alert("End time is "+endTime);
});

DEMO (JSFiddle)

JSBin