0

演示在 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);
            });   
4

1 回答 1

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);
});

演示(JSFiddle)

JSBin

于 2012-08-21T15:56:58.637 回答