Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.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中日期对象数组中任意时间的时间百分比_Javascript_Algorithm_Date - Fatal编程技术网

如何计算Javascript中日期对象数组中任意时间的时间百分比

如何计算Javascript中日期对象数组中任意时间的时间百分比,javascript,algorithm,date,Javascript,Algorithm,Date,我有一个Javascript日期对象的排序数组,该数组的长度始终为14个条目。在伪文本中: dates ["label1"] = date object 1, ["label2"] = date object 2, ... ["label14"] = date object 14 虽然它们是按日期排序的,但日期的分布并不均匀。例如,条目1和条目2之间可能为1小时,而条目5和条目6之间可能为几分钟或几小时 我的挑战是找到一种算法,对于任何给定的输入日期,都可以从该数组中找到以下内容: 上一日期

我有一个Javascript日期对象的排序数组,该数组的长度始终为14个条目。在伪文本中:

dates
["label1"] = date object 1,
["label2"] = date object 2,
...
["label14"] = date object 14
虽然它们是按日期排序的,但日期的分布并不均匀。例如,条目1和条目2之间可能为1小时,而条目5和条目6之间可能为几分钟或几小时

我的挑战是找到一种算法,对于任何给定的输入日期,都可以从该数组中找到以下内容:

  • 上一日期在数组中的位置
  • 下一个日期在数组中的位置
  • 我们的输入日期将介于上述日期之间,我想知道2个点之间的时间百分比。例如,恰好在两个日期之间,将返回50%

我不喜欢问“请为我编写此代码”的问题,但我真的不擅长编写有效的算法,并且在查找现有算法时失败。

由于数组已排序,因此上一个日期位于数组中的上一个位置,而下一个日期位于数组中的下一个位置

要确定百分比,只需使用以下公式:

var percentage = (date - prev) / (next - prev);
这将导致一个介于0和1之间的数字,因此如果需要介于0和100%之间,只需乘以100即可

例如:

var dates = [];
for (var i=0; i<14; i++) { dates.push(new Date(1392119656126 + i*i*10000000)) }
for (var i = 1; i<dates.length-1; i++) { 
  var percentage = ((dates[i]-dates[i-1])/(dates[i+1]-dates[i-1])) * 100;
  console.log('P', dates[i-1], 'C', dates[i], 'N', dates[i+1], 'PERC', percentage);
}
> var dates=[new Date(0), new Date(100), new Date(500)]
> index(dates, new Date(-1))
-1
> index(dates, new Date(0))
0
> index(dates, new Date(50))
0.5
> index(dates, new Date(70))
0.7
> index(dates, new Date(100))
1
> index(dates, new Date(150))
1.125
> index(dates, new Date(500))
2
> index(dates, new Date(600))
3
var日期=[];
对于(var i=0;iou,可使用a表示该值。改编自:

因此,当您从这样一个
索引调用中得到一个结果时,您会

  • 首先检查它是否在边界之外
    res<0 | | res>=length
  • 通过
    Math.floor(res)
  • 通过
    Math.ceil(res)
  • 通过
    (res%1)*100获取它们之间的百分比
> var dates=[new Date(0), new Date(100), new Date(500)]
> index(dates, new Date(-1))
-1
> index(dates, new Date(0))
0
> index(dates, new Date(50))
0.5
> index(dates, new Date(70))
0.7
> index(dates, new Date(100))
1
> index(dates, new Date(150))
1.125
> index(dates, new Date(500))
2
> index(dates, new Date(600))
3