我有两个 UNIX 时间戳,我想将它们加在一起以创建一个“总”时间,但我还想计算每个时间戳的总价值百分比。例如:
var x = 123456;
var y = 1234;
var total = 124690;
x is % of total, y is % of total.
我怎样才能在javascript中实现这一点?在以某种方式计算百分比之前,我是否需要将 UNIX 时间戳转换为标准格式?
我有两个 UNIX 时间戳,我想将它们加在一起以创建一个“总”时间,但我还想计算每个时间戳的总价值百分比。例如:
var x = 123456;
var y = 1234;
var total = 124690;
x is % of total, y is % of total.
我怎样才能在javascript中实现这一点?在以某种方式计算百分比之前,我是否需要将 UNIX 时间戳转换为标准格式?
既然您说“我想加在一起以创建一个'总'时间”,我假设您的意思只是“时间长度”,可能以秒为单位,即您想知道两个事件中的每一个都有多少百分比。这些不是 Unix 时间戳(它是日期的整数表示,即自 Unix 纪元开始以来的时间长度)。
做这个:
var x = 123456;
var y = 1234;
var total = x + y;
alert("x is " + Math.round(x / total * 100) + "% of total, y is " + Math.round(y / total * 100) + "% of total");
如果您想要更多的百分比数字,例如两位,请使用
alert("x is " + (Math.round(x / total * 10000) / 100) + "% of total, y is " + (Math.round(y / total * 10000) / 100) + "% of total");