【问题标题】:find number from div tag and calculate the total从 div 标签中查找数字并计算总数
【发布时间】:2013-01-13 18:12:09
【问题描述】:

我有一个html文本如下

<div class="co"><b>op</b> (11:32:20): here some string</div>
<div class="cv"><b>cl</b> (11:32:42): here some another string</div>
<div class="co"><b>op</b> (11:33:15): another...</div>
<div class="co"><b>cl</b> (11:33:36): another...</div>
<div class="cv"><b>op</b> (11:33:45): another...</div>
<div class="co"><b>cl</b> (11:34:46): another...</div>
<div class="cv"><b>op</b> (11:35:00): another...</div>
<div class="co"><b>cl</b> (11:37:19): another...</div>

如何从上面的示例中获取第一个 div 编号和最后一个 div 编号。在这种情况下,第一个 div 编号 is 11:32:20 和最后一个 div 编号 11:37:19 我想计算差异,例如 11:37:19 减去 11:32:20

希望清楚。

【问题讨论】:

  • 那些不是数字,而是时代。
  • 没有因为缺少我的javascript。希望有人指引我正确的方向来寻找或解决我的问题
  • 方向 1:学习 JavaScript。方向2:聘请程序员。任你选。 SO 不是“为我编程”网站。

标签: javascript jquery html


【解决方案1】:

Another alternative:

// parses the hh:mm:ss out of the div's text
function timeStrToDate(str){
    // look for hh:mm:ss and extract it
    var m = str.match(/(\d{1,2}:\d{1,2}:\d{1,2})/);
    if(m){
        // we were able to locate it, so convert it to a Date
        // (we only care about time, so use epoc as a ref. point)
        return new Date('Thu, 01 Jan 1970 ' + m[1]);
    }
    // return an empty time by default
    return new Date('Thu, 01 Jan 1970');
}
// converts straight seconds in to hh:mm:ss
function secondsToTime(sec){
    var r = [], d
    // go through by hours, minutes then seconds
    // and divide then subtract each time
    var exp = [3600,60,1];
    for (e = 0; e < exp.length; e++){
        if (sec >= exp[e]){
            d = Math.floor(sec / exp[e]);
            sec -= d * exp[e];
            r.push(d < 10 ? '0'+d : d); // pad it so 9 becomes 09
        }
    }
    return r.join(':');
}

// the actual jQuery code

// grab all divs
var $divs = $('div.co,div.cv');
// Grab first element, get text, and parse the time out
var $first = timeStrToDate($divs.first().text()),
    // do the same with the last
    $last = timeStrToDate($divs.last().text()),
    // get the difference (returned in seconds elapsed)
    $delta = Math.abs($last - $first) / 1e3; // `/1e3` -> milliseconds to seconds

 // here's the end result, 4:59 in this case
console.log(secondsToTime($delta));

【讨论】:

  • 非常感谢。我只有一个疑问,如果新日期从 1 月 1 日开始到 1 月 2 日结束,那么脚本会产生错误的时间吗?因为函数 timeStrToDate 总是在 1 月 1 日。
  • @memo:您的示例不包括日期,只是时间。你是说可能包含翻转或日期?如果是这样,请用示例更新您的问题,我可以进行调整。
【解决方案2】:
function getSeconds(time){
    var tokens = time.split(":");
    var seconds = parseInt(tokens[2]);
    seconds += parseInt(tokens[1]) * 60;
    seconds += parseInt(tokens[0]) * 60 *60;
    return seconds;
}

var divs = $(".co,.cv");
var firstHtml = $(divs.eq(0)).html();
var lastHtml = $(divs.eq(divs.length-1)).html();
var firstValue = firstHtml.substring(firstHtml.indexOf("(") +1, firstHtml.indexOf(")"));
var lastValue = lastHtml.substring(firstHtml.indexOf("(") +1, firstHtml.indexOf(")"));

var diff = getSeconds(lastValue) - getSeconds(firstValue);

console.log(diff);

示例:http://jsfiddle.net/CRQFk/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-19
    • 1970-01-01
    • 1970-01-01
    • 2022-07-31
    • 2021-06-23
    • 2012-08-20
    • 1970-01-01
    相关资源
    最近更新 更多