如何查找迄今为止的7-14天的日期

时间:2019-06-25 14:53:04

标签: javascript arrays momentjs

我的数组充满了日期。我需要计算所有早于7天且比现在小14天的日期。

示例:

Today is 25/06/2019
Date1: 17/06/2019 - Correct
Date2: 23/06/2019 - Incorrect

为此,我打算使用moment.js

数组中的日期格式为MM / DD / YYYY HH:mm:ss

var res = {sevendays:0};
const morethan7 = moment.utc().add(7, 'day').endOf('day');
const lessthan14 = moment.utc().add(14, 'day').endOf('day');


agedArray.forEach((date)=>{
    var agedtime = moment.utc(date, "MM/DD/YYYY HH:mm:ss");

    if ((agedtime.isBetween(morethan7)) && (agedtime.isBefore(lessthan14)) )
        res.sevendays++;

不幸的是,结果为0。我认为我今天需要指出,但是不知道如何做。

编辑1: ageedArray:

1: "05/20/2019 15:22:58"
2: "06/06/2019 16:42:19"
3: "06/06/2019 17:33:18"
4: "06/07/2019 18:52:09"
5: "06/08/2019 09:32:49"
6: "06/08/2019 10:25:02"
7: "06/10/2019 17:43:22"
8: "06/12/2019 19:03:22"
9: "06/15/2019 07:28:44"
10: "06/15/2019 09:19:34"
11: "06/15/2019 07:41:55"
12: "06/15/2019 09:57:29"
13: "06/15/2019 10:43:41"
14: "06/15/2019 11:01:06"
15: "06/15/2019 12:11:29"
16: "06/15/2019 11:59:45"
17: "06/15/2019 12:26:51"
18: "06/15/2019 14:20:59"
19: "06/17/2019 17:19:41"
20: "06/17/2019 17:25:46"
21: "06/17/2019 17:29:45"
22: "06/17/2019 17:30:49"
23: "06/17/2019 17:33:55"
24: "06/17/2019 17:07:52"
25: "06/17/2019 17:48:49"
26: "06/17/2019 18:01:09"
27: "06/17/2019 18:24:32"
28: "06/17/2019 18:28:32"
29: "06/18/2019 17:25:45"
30: "06/18/2019 17:37:30"
31: "06/18/2019 17:46:49"
32: "06/18/2019 17:51:23"
33: "06/18/2019 18:05:15"
34: "06/18/2019 18:10:21"
35: "06/18/2019 18:21:09"
36: "06/18/2019 17:54:39"
37: "06/18/2019 18:00:57"
38: "06/18/2019 18:12:34"
39: "06/18/2019 18:13:49"
40: "06/18/2019 18:31:27"
41: "06/18/2019 19:14:11"
42: "06/19/2019 17:06:39"
43: "06/19/2019 17:34:16"
44: "06/19/2019 17:37:25"
45: "06/19/2019 17:05:14"
46: "06/19/2019 17:18:30"
47: "06/19/2019 17:34:54"
48: "06/19/2019 17:43:17"
49: "06/19/2019 17:46:32"
50: "06/19/2019 17:48:10"
51: "06/19/2019 17:50:40"
52: "06/20/2019 18:47:59"
53: "06/20/2019 19:04:12"
54: "06/20/2019 19:09:11"
55: "06/20/2019 20:13:04"
56: "06/21/2019 17:18:22"
57: "06/21/2019 18:38:30"
58: "06/22/2019 07:41:11"
59: "06/22/2019 07:44:51"
60: "06/22/2019 08:16:10"
61: "06/22/2019 07:51:27"
62: "06/22/2019 07:55:30"
63: "06/22/2019 08:01:44"
64: "06/24/2019 18:00:12"
65: "06/24/2019 17:22:03"
66: "06/24/2019 17:23:59"
67: "06/24/2019 17:50:38"
68: "06/24/2019 17:56:12"
69: "06/24/2019 17:58:08"
70: "06/24/2019 18:03:22"
71: "06/24/2019 18:07:28"

2 个答案:

答案 0 :(得分:3)

您使用方法.add到达,但您希望使用旧日期。

改为使用.subtract

例如

const morethan7 = moment().subtract(7, 'day');
const lessthan14 = moment().subtract(14, 'day');

就像@Cid所说的那样,将if条件替换为:

if (agedtime.isBetween(morethan7, lessthan14))

答案 1 :(得分:1)

是否有原因,纯JavaScript Array.prototype.filter()对您不起作用?

const src = ['05/20/2019 15:22:58','06/06/2019 16:42:19','06/06/2019 17:33:18','06/07/2019 18:52:09','06/08/2019 09:32:49','06/08/2019 10:25:02','06/10/2019 17:43:22','06/12/2019 19:03:22','06/15/2019 07:28:44','06/15/2019 09:19:34','06/15/2019 07:41:55','06/15/2019 09:57:29','06/15/2019 10:43:41','06/15/2019 11:01:06','06/15/2019 12:11:29','06/15/2019 11:59:45','06/15/2019 12:26:51','06/15/2019 14:20:59','06/17/2019 17:19:41','06/17/2019 17:25:46','06/17/2019 17:29:45','06/17/2019 17:30:49','06/17/2019 17:33:55','06/17/2019 17:07:52','06/17/2019 17:48:49','06/17/2019 18:01:09','06/17/2019 18:24:32','06/17/2019 18:28:32','06/18/2019 17:25:45','06/18/2019 17:37:30','06/18/2019 17:46:49','06/18/2019 17:51:23','06/18/2019 18:05:15','06/18/2019 18:10:21','06/18/2019 18:21:09','06/18/2019 17:54:39','06/18/2019 18:00:57','06/18/2019 18:12:34','06/18/2019 18:13:49','06/18/2019 18:31:27','06/18/2019 19:14:11','06/19/2019 17:06:39','06/19/2019 17:34:16','06/19/2019 17:37:25','06/19/2019 17:05:14','06/19/2019 17:18:30','06/19/2019 17:34:54','06/19/2019 17:43:17','06/19/2019 17:46:32','06/19/2019 17:48:10','06/19/2019 17:50:40','06/20/2019 18:47:59','06/20/2019 19:04:12','06/20/2019 19:09:11','06/20/2019 20:13:04','06/21/2019 17:18:22','06/21/2019 18:38:30','06/22/2019 07:41:11','06/22/2019 07:44:51','06/22/2019 08:16:10','06/22/2019 07:51:27','06/22/2019 07:55:30','06/22/2019 08:01:44','06/24/2019 18:00:12','06/24/2019 17:22:03','06/24/2019 17:23:59','06/24/2019 17:50:38','06/24/2019 17:56:12','06/24/2019 17:58:08','06/24/2019 18:03:22','06/24/2019 18:07:28'];

const res = src.filter(dateStr => (new Date() - new Date(dateStr))>7*86400000 && (new Date() - new Date(dateStr))<14*86400000);

console.log(res);