Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中将负秒转换为hh:mm:ss_Javascript - Fatal编程技术网

如何在javascript中将负秒转换为hh:mm:ss

如何在javascript中将负秒转换为hh:mm:ss,javascript,Javascript,我正在寻找将负秒转换为hh:mm:ss格式的解决方案。 我想将-60显示为-00:01:00。 这就是我所做的 var res = d.toString().split("-"); var currentValue = res[1]; var currentDuration = "-" + getDuration(currentValue); function getDuration(d) { var h = Math.floor(d / 3600); var m = Math.floor(d

我正在寻找将负秒转换为hh:mm:ss格式的解决方案。 我想将-60显示为-00:01:00。 这就是我所做的

var res = d.toString().split("-");
var currentValue = res[1];
var currentDuration = "-" + getDuration(currentValue);

function getDuration(d) {
var h = Math.floor(d / 3600);
var m = Math.floor(d % 3600 / 60);
h = h < 10 ? '0' + h : h;
m = m < 10 ? '0' + m : m;
return h + ':' + m;
var res=d.toString().split(“-”);
var currentValue=res[1];
var currentDuration=“-”+getDuration(currentValue);
函数getDuration(d){
var h=数学楼层(d/3600);
var m=数学下限(d%3600/60);
h=h<10?'0'+h:h;
m=m<10?'0'+m:m;
返回h+':'+m;
}


有更好的方法吗?

输入是整数,可以是负数或正数:

function nSecToTime(s) { 
  let seconds = s
  s = Math.abs(s)

  let t = [0, 0, 0]
  let r = s % 3600

  t[0] = Math.floor(s / 3600)
  t[1] = Math.floor(r / 60)
  r = r % 60
  t[2] = r

  return (seconds < 0 ? "-" : "") + (t[0] < 10 ? "0" : "") + t[0]+":"+(t[1]<10?"0"+t[1]:t[1])+":"+(t[2]<10?"0"+t[2]:t[2])
}

nSecToTime(-58); // returns -00:00:58
nSecToTime(-8452); // returns -02:20:52
nSecToTime(2334); // returns 00:38:54
函数nsecottime(s){
让秒=秒
s=数学。abs(s)
设t=[0,0,0]
设r=s%3600
t[0]=数学层(s/3600)
t[1]=数学楼层(r/60)
r=r%60
t[2]=r

返回(秒数<0?-“:”)+(t[0]<10?“0:”)+t[0]+“:”+(t[1]你能转换绝对值/正值并在其前面添加负号吗?实际上,我从服务器获取负值。我正在尝试找到一种方法来转换它,而不删除前导的减号,转换并添加减号。到目前为止你已经尝试了什么?为什么不想删除减号?区别是什么在转换-60到分钟和60到分钟之间?这里没有什么特定于负面的东西,只是如何将秒转换为hh:mm:ss格式,然后在输出前添加一个-。你似乎很困惑。我知道这种转换方式,这就是我所做的。问题是我想知道是否有转换的选项直接rt。就这样。58秒怎么等于00:01:09?