2

我想获得有关我的 jquery 的帮助。我对 HTML 元素中这些时间的返回值有疑问。我正在计算时间 1 和时间 2 之间的时间,它将持续多长时间,例如:下午 4:30 带走下午 6:30,返回值为 2.00。

下面是 HTML 的样例:

<span id='time1'>4:30 PM</span> - <span id='title1'>The Scorpion King</span><br></br>
<span id='time2'>6:30 PM</span> - <span id='title2'>Gone in Sixty Seconds</span><br></br>
<span id='time3'>9:00 PM</span> - <span id='title3'>The Fast and the Furious</span><br></br>
<span id='time4'>11:30 PM</span> - <span id='title4'>The Fast and the Furious</span><br></br>
<span id='time5'>2:00 AM</span> - <span id='title5'>The Scorpion King</span><br></br>
<span id='time6'>4:00 AM</span> - <span id='title6'>New Sheer Cover Studio mineral makeup</span><br></br>
<span id='time7'>8:49 PM</span> - <span id='title7'>Tamra's O.C. Wedding</span><br></br>

在这种情况下,我有一个返回值,当我计算出应该在 2 小时内显示“2.00”的返回值时,它将从 time1 和 time2 显示“-10.00”。还有其他值可以做同样的事情,这会让我混淆时间将持续几个小时。我对任何其他返回值都没有问题,当我在不同的时间锻炼时,我会得到 1.00、1.30、2.00、2.30..等。我想计算出它会持续多长时间而不得到返回值'-10.00','-11.00'或任何使用类似'1.00','2.00'的东西, '3.00'...等之前做某事。

我已经尝试过这样的代码,但它会得到错误的返回值,例如“-10.00”、“-11.00”或任何它应该是 1.00、2.00 或其他的东西。

function getSchedule($link,j)
{
  var widthval =  850;
  var parts = $link.split("/");
  var links = parts[parts.length-1];
  var programlength = 0;

  $.ajax({
  url:$.trim(links),
  type:'GET',
  data:'',
  success: function(data)
  {
    var $data = $(data);        
    var title1 = $data.filter("#title1").html();
    var title2 = $data.filter("#title2").html();

    var time1 = $data.filter("#time1").html();
    var time2 = $data.filter("#time2").html();

    time1 = time1.split(" ");
    var time1AMPM = time1[1];
    time1 = time1[0].split(":");
    time1= time1[0]+'.'+time1[1];

    if($.trim(time1AMPM) == 'AM' && time1<12)
      time1 = parseFloat(time1)+12;
    else if($.trim(time1AMPM) == 'PM' && time1<12)
      time1 = parseFloat(time1)+12;

      time2 = time2.split(" ");
      var time2AMPM = time2[1];
      time2 = time2[0].split(":");
      time2= time2[0]+'.'+time2[1];
    if($.trim(time2AMPM) == 'AM' && time2<12)
      time2 = parseFloat(time2)+12;  
    else if($.trim(time2AMPM) == 'PM' && time2<12)
      time2 = parseFloat(time2)+12;

      var difftime2time1 = (parseFloat(time2) - parseFloat(time1)).toFixed(2);
      var difftime3time2 = (parseFloat(time3) - parseFloat(time2)).toFixed(2);

      if(isNaN(difftime2time1))
      {
        difftime2time1=0;
      }
      if(isNaN(difftime3time2))
      {
        difftime3time2=0;
      }
      var currenttotal = 0; 
      var firstele = ((j-1)*1)+1; // how many programme i want to output in per block
      var lastele = parseInt(firstele)+1; // how many programme i want to output AFTER the firstele
      var k = 1;
      var programlength = 0;

      for(;firstele <= lastele;firstele++)
      {
        var nexttimedate = parseInt(k)+1;
        var programlength = eval('difftime'+nexttimedate+'time'+k);
        //alert(programlength);

      if(programlength >= 1.00 && programlength <= 1.29)
      {
        $('#programe'+firstele).addClass("span1hr");
        width[j]=517;
      }

      if(programlength >= 1.30 && programlength <= 1.80)
      {
        $('#programe'+firstele).addClass("span1_5hr");
        width[j]=784;
      }

      if(programlength >= 1.81 && programlength <= 1.99)
      {
        $('#programe'+firstele).addClass("span2hr");
        width[j]=784;
      }

      if(programlength >= 2.00 && programlength <= 2.29)
      {
        $('#programe'+firstele).addClass("span2hr");
        width[j]=1051;
      }

      if(programlength >= 2.30 && programlength <= 2.80)
      {
        $('#programe'+firstele).addClass("span2_5hr");
        width[j]=1318;
      }

      //sort this out--------1 hour
      if(programlength >= 13.00 && programlength <= 13.29)
      {
        $('#programe'+firstele).addClass("span1hr");
        width[j]=517;
      }  

      //sort this out--------1 hour and 30 mins
      if(programlength >= 13.30 && programlength <= 13.80)
      {
        $('#programe'+firstele).addClass("span1_5hr");
        width[j]=517;
      }

      //sort this out--------2 hour
      if(programlength >= 14.00 && programlength <= 14.29)
      {
        $('#programe'+firstele).addClass("span2hr");
        width[j]=1585;
      }

      //sort this out-------- 2 hours and 30 mins
      if(programlength >= 14.30 && programlength <= 14.80)
      {
        $('#programe'+firstele).addClass("span2_5hr");
        width[j]=1585;
      } 

      if(programlength == -10.00) //sort this out  ---- 2 hour program
      {
        $('#programe'+firstele).addClass("span2hr");
        width[j]=1051;
      }
      currenttotal++;
      pgmcontent[firstele] = eval('title'+k);
      k++;      
    }
    checksum+=j;

    if(checksuminit == checksum)
    {
      for(var ii=1;ii<width.length-1;ii++)
      {
        widthval+=width[ii];
      }

      for(var jj=1;jj <= pgmcontent.length-1;jj++)
      {
        $('#programe'+jj).html(pgmcontent[jj]);
      }

      for(var kk=1;kk <= imagecontent.length-1;kk++)
      {
        $('#image'+kk).html(imagecontent[kk]);
      }
      $("body").find('.rowSubPgm').each(function(index) {
      $(this).css( "width", widthval+"px");
      });
    }
  }   
  });
}

我无法发布 jsfiddle 以作为示例显示,因为当我按时锻炼时不会显示警报。不过,我很高兴发布我的网站链接,这样您就可以看到具有差异值“1.00、2.00、“13.00”、“14.00”和“-10.00”的警报消息。你会知道我在说什么。

我的网站链接:

4

2 回答 2

1

如果您有一个时间从上午 11 点开始到下午 1 点结束,那么您的代码当前将上午和下午都增加了 12 小时,导致 13 - 23 = -10

时间是 AM 现在是:

if($.trim(time1AMPM) == 'AM' && time1<12)
      time1 = parseFloat(time1)+12;

尝试:

if($.trim(time1AMPM) == 'AM')
      time1 = parseFloat(time1);

time2 也一样。

然后,您还需要添加检查超过午夜的程序。例如,从 2300 开始,到 0100 结束。最简单的做法是:

if (difftime2time1 < 0) difftime2time1 += 24; // add on 24 hours
于 2013-09-07T22:13:34.317 回答
0

感谢您的快速回复,大部分问题都已解决,但有些值显示为“-9.00”、“-8.30”、“-11.00”。我不知道如何摆脱它并使用 1.00、2.00 或其他任何东西。

这是当前代码:

time1 = time1.split(" ");
var time1AMPM = time1[1];
time1 = time1[0].split(":");
time1= time1[0]+'.'+time1[1];

if($.trim(time1AMPM) == 'AM')
  time1 = parseFloat(time1);
else if($.trim(time1AMPM) == 'PM')
  time1 = parseFloat(time1);

  time2 = time2.split(" ");
  var time2AMPM = time2[1];
  time2 = time2[0].split(":");
  time2= time2[0]+'.'+time2[1];
if($.trim(time2AMPM) == 'AM')
  time2 = parseFloat(time2);  
else if($.trim(time2AMPM) == 'PM')
  time2 = parseFloat(time2);

  time3 = time3.split(" ");
  var time3AMPM = time3[1];
  time3 = time3[0].split(":");
  time3 = time3[0]+'.'+time3[1];
if($.trim(time3AMPM) == 'AM')
  time3 = parseFloat(time3);
else if($.trim(time3AMPM) == 'PM')
  time3 = parseFloat(time3);

  time4 = time4.split(" ");
  var time4AMPM = time4[1];
  time4 = time4[0].split(":");
  time4= time4[0]+'.'+time4[1];
if($.trim(time4AMPM) == 'AM')
  time4 = parseFloat(time4);
else if($.trim(time4AMPM) == 'PM')
  time4 = parseFloat(time4);

  time5 = time5.split(" ");
  var time5AMPM = time5[1];
  time5 = time5[0].split(":");
  time5= time5[0]+'.'+time5[1];
if($.trim(time5AMPM) == 'AM')
  time5 = parseFloat(time5);  
else if($.trim(time5AMPM) == 'PM')
  time5 = parseFloat(time5);

  time6 = time6.split(" ");
  var time6AMPM = time6[1];
  time6 = time6[0].split(":");
  time6= time6[0]+'.'+time6[1];
if($.trim(time6AMPM) == 'AM')
  time6 = parseFloat(time6);  
else if($.trim(time6AMPM) == 'PM')
  time6 = parseFloat(time6);


  var difftime2time1 = (parseFloat(time2) - parseFloat(time1)).toFixed(2);
  var difftime3time2 = (parseFloat(time3) - parseFloat(time2)).toFixed(2);
  var difftime4time3 = (parseFloat(time4) - parseFloat(time3)).toFixed(2);
  var difftime5time4 = (parseFloat(time5) - parseFloat(time4)).toFixed(2);
  var difftime6time5 = (parseFloat(time6) - parseFloat(time5)).toFixed(2);

  if(isNaN(difftime2time1))
  {
    difftime2time1=0;
  }
  if(isNaN(difftime3time2))
  {
difftime3time2=0;
  }
  if(isNaN(difftime4time3))
  {
difftime4time3=0;
  }
  if(isNaN(difftime5time4))
  {
difftime5time4=0;
  }
  if(isNaN(difftime6time5))
  {
difftime6time5=0;
  }
  var currenttotal = 0; 
  var firstele = ((j-1)*5)+1; // how many programme i want to output in per block
  var lastele = parseInt(firstele)+4; // how many programme i want to output AFTER the firstele
  var k = 1;
  var programlength = 0;

你知道为什么有些返回值仍然显示'-9.00'、'-8.30'、'-11.00'吗?我怎样才能让返回值显示像'3.00'、'3.30'和'1.00'这样的东西'而不是使用'-9.00'、'-8.30'、'-11.00'?

于 2013-09-07T23:14:16.147 回答