【问题标题】:Display timer correctly (3:45) based on number of seconds根据秒数正确显示计时器 (3:45)
【发布时间】:2015-12-21 03:33:48
【问题描述】:

我正在开发一个网站并使用 socket.io。我有物品拍卖,我在我的数据库中设置了每次拍卖结束的时间(unix 时间戳)。当用户对该项目出价时,如果结束时间少于 20 秒,那么数据库上的时间必须更改才能恢复到剩余 20 秒。在每次新出价时,剩下的秒数将是 20 秒。 话虽如此,这就是我们所拥有的:

  • 客户端连接并从服务器获取最后时间(服务器从数据库中获取并告诉客户端)

  • 然后客户端必须显示剩余时间(当前显示 xxx 秒)(这里是我需要帮助的地方。)

  • 当用户出价时,服务器会检查计时器是否低于 20 秒,如果是,服务器会在数据库中添加 UNIX_TIMESTAMP(NOW())+20

长话短说,我需要的是 javascript 来计算数据库存储的时间戳减去当前的 unix 时间戳(它已经这样做了),然后将其转换为 0:00... 假设数据库时间 - 当前时间等于 600 秒,我应该如何将其变成 5:00 输出? 谢谢!

解决方案: 对不起,伙计们,这真的很容易解决,经过一些建议后我找到了解决方案。对于那些面临同样“问题”的人来说,这里是我找到的解决方案,它再简单不过了

var time= 300;
var minutes = "0" + Math.floor(time / 60);
var seconds = "0" + (time - minutes * 60);
return minutes.substr(-2) + ":" + seconds.substr(-2);

感谢在此线程上给出此答案的用户:Javascript seconds to minutes and seconds

【问题讨论】:

  • 您希望代码将秒数转换为minutes:seconds 格式 - 对吗?
  • 是的,请=],任何帮助将不胜感激
  • 您知道为什么我的服务器从我的客户端返回不同的 unix 时间戳吗? unix 时间戳不是全球都一样吗?

标签: javascript time


【解决方案1】:

您可以将秒数以 60 为模。结果以分钟为单位,余数以秒为单位。确保您考虑了前导 0。可能有某种字符串格式,但我不知道。不过,Moment.js 也会使这变得非常简单,只需获取当前时间,添加秒数,然后将其格式选项之一与 m:ss 一起使用。

这里有很多例子可供moment.js参考。这是一个例子:How to convert seconds to HH:mm:ss in moment.js

【讨论】:

    【解决方案2】:

    有一个简单快速的解决方案可以将秒数格式化为M:SS(因此您的问题要求没有零填充的分钟数):

    function fmtMSS(s){return(s-(s%=60))/60+(9<s?':':':0')+s}
    

    该函数接受 任一 Number(首选) String(2 个转换“惩罚”,您可以通过在前面加上 + s的函数调用参数如:fmtMSS(+strSeconds)),表示正整数秒s作为参数。

    例子:

    fmtMSS(    0 );  //   0:00
    fmtMSS(   '8');  //   0:08
    fmtMSS(    9 );  //   0:09
    fmtMSS(  '10');  //   0:10
    fmtMSS(   59 );  //   0:59
    fmtMSS( +'60');  //   1:00
    fmtMSS(   69 );  //   1:09
    fmtMSS( 3599 );  //  59:59
    fmtMSS('3600');  //  60:00
    fmtMSS('3661');  //  61:01
    fmtMSS( 7425 );  // 123:45
    

    细分:

    function fmtMSS(s){   // accepts seconds as Number or String. Returns m:ss
      return( s -         // take value s and subtract (will try to convert String to Number)
              ( s %= 60 ) // the new value of s, now holding the remainder of s divided by 60 
                          // (will also try to convert String to Number)
            ) / 60 + (    // and divide the resulting Number by 60 to give minutes
                          // (can never result in a fractional value = no need for rounding)
                          // to which we concatenate a String (converts the Number to String)
                          // who's reference is chosen by the conditional operator:
              9 < s       // if    seconds is larger than 9
              ? ':'       // then  we don't need to prepend a zero
              : ':0'      // else  we do need to prepend a zero
            ) + s ;       // and we add Number s to the string (converting it to String as well)
    }
    

    注意:可以通过在返回表达式前面加上 (0&gt;s?(s=-s,'-'):'')+ 来添加负范围(实际上,(0&gt;s?(s=-s,'-'):0)+ 也可以)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-06
      相关资源
      最近更新 更多