Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.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_Datetime_Ecmascript 6_Datetime Format - Fatal编程技术网

在javascript中删除比当前时间早一年的时间戳属性

在javascript中删除比当前时间早一年的时间戳属性,javascript,datetime,ecmascript-6,datetime-format,Javascript,Datetime,Ecmascript 6,Datetime Format,我有一个时间戳为毫秒的对象: const indexes = { index1: 1490659200000 // 2017-03-28 index2: 1490659200000 // 2017-03-28 index3: 1498608000000 // 2017-06-28 } 如何删除时间戳比当前时间早一年的索引新日期().getTime()-1522269838207。 实际上,通过对象循环并删除超过1年的时间戳的更快方法是什么。也许转换成yyy-

我有一个时间戳为毫秒的对象:

const indexes = {
      index1: 1490659200000 // 2017-03-28
      index2: 1490659200000 // 2017-03-28
      index3: 1498608000000 // 2017-06-28
}
如何删除时间戳比当前时间早一年的索引<代码>新日期().getTime()-
1522269838207
。 实际上,通过对象循环并删除超过1年的时间戳的更快方法是什么。也许转换成yyy-mm-dd比用yyy-mm-dd进行比较更好

Object.keys(indexes).forEach(i => {
   if (indexes[i] < new Date().getTime())
     delete indexes[i];
})
Object.keys(索引).forEach(i=>{
if(索引[i]
另一种选择是使用功能
reduce
以及操作
对象中的一年-日期

这种方法将创建一个日期不超过一年的新数组

const index={index1:1490659200000,index2:1490659200000,index3:149860000000}
var oneYearAgo=新日期();
oneYearAgo.setFullYear(oneYearAgo.getFullYear()-1);
var result=Object.keys(index).reduce((a,c)=>{
如果(一年前的.getTime()-index[c]>0)a.push(index[c]);
返回a;
}, [])

控制台日志(结果)另一种选择是使用功能
reduce
以及操作
对象中的一年-日期

这种方法将创建一个日期不超过一年的新数组

const index={index1:1490659200000,index2:1490659200000,index3:149860000000}
var oneYearAgo=新日期();
oneYearAgo.setFullYear(oneYearAgo.getFullYear()-1);
var result=Object.keys(index).reduce((a,c)=>{
如果(一年前的.getTime()-index[c]>0)a.push(index[c]);
返回a;
}, [])

控制台日志(结果)这取决于“超过一年”的确切程度。如果我们认为那一年有365天,那么:

const indexes = {
    index1: 1490659200000, // 2017-03-28
    index2: 1490659200000, // 2017-03-28
    index3: 1498608000000 // 2017-06-28
}

var currentDate = new Date();
var year = 365 * 24 * 60 * 60 * 1000;

for (var index in indexes) {
    if (indexes[index] < (currentDate - year)) delete indexes[index];
}
const索引={
index1:1490659200000,//2017-03-28
index2:1490659200000,//2017-03-28
索引3:149860000000//2017-06-28
}
var currentDate=新日期();
风险年=365*24*60*60*1000;
for(索引中的var索引){
如果(索引[索引]<(当前日期-年份))删除索引[索引];
}

这取决于“超过一年”的确切程度。如果我们认为那一年有365天,那么:

const indexes = {
    index1: 1490659200000, // 2017-03-28
    index2: 1490659200000, // 2017-03-28
    index3: 1498608000000 // 2017-06-28
}

var currentDate = new Date();
var year = 365 * 24 * 60 * 60 * 1000;

for (var index in indexes) {
    if (indexes[index] < (currentDate - year)) delete indexes[index];
}
const索引={
index1:1490659200000,//2017-03-28
index2:1490659200000,//2017-03-28
索引3:149860000000//2017-06-28
}
var currentDate=新日期();
风险年=365*24*60*60*1000;
for(索引中的var索引){
如果(索引[索引]<(当前日期-年份))删除索引[索引];
}