简体   繁体   中英

Getting weekdays that falls between present day and last seven days in javaScript

I have an array of timestamps which i want to get the present day of the weekdays from the timestamp. I have the weekdays already but I'm getting all weekdays in the timestamp. (In this case all Saturdays). Please how do i get weekdays that falls within the last seven days and not all Saturdays here.

I need my result to be

[

"2022-01-08T05:33:11.000Z",
"2022-01-08T05:33:11.000Z"

]

instead of having all the Saturdays in the Array I need just within a week

Instead of having this

[
  2021-09-18T11:07:44.000Z,
  2021-11-06T20:43:16.000Z,
  2021-09-18T16:15:08.000Z,
  2022-01-08T05:33:11.000Z,
  2022-01-08T05:33:11.000Z
]

My code below

 const availabledates = [ "2021-09-28 15:05:40", "2021-12-02 14:15:54", "2021-09-21 12:34:47", "2021-09-22 14:40:08", "2021-12-20 13:05:08", "2021-09-28 12:20:50", "2021-12-10 08:27:38", "2021-09-17 07:37:36", "2021-11-08 14:38:57", "2021-11-22 17:59:48", "2021-11-11 13:29:03", "2021-09-18 12:07:44", "2021-09-24 16:43:42", "2021-09-21 11:50:54", "2021-10-06 14:29:10", "2021-12-02 18:14:11", "2021-12-20 22:11:36", "2021-10-29 15:10:38", "2021-09-16 21:42:08", "2021-09-24 08:05:24", "2021-09-24 11:26:52", "2021-09-22 10:09:36", "2021-09-22 13:57:35", "2021-09-21 11:03:50", "2021-09-23 16:27:55", "2021-10-29 15:02:10", "2021-09-21 15:29:05", "2021-11-14 16:54:55", "2021-11-19 16:21:38", "2021-09-17 19:22:05", "2021-10-26 10:31:38", "2021-10-03 14:56:07", "2021-11-08 12:12:48", "2021-11-29 16:29:05", "2021-10-28 09:38:54", "2021-09-23 17:19:37", "2021-09-21 13:24:41", "2021-11-06 21:43:16", "2021-11-03 08:39:31", "2021-10-08 11:18:59", "2021-10-28 09:15:47", "2021-11-15 15:20:22", "2021-10-27 13:22:40", "2021-09-22 12:39:06", "2021-09-22 14:12:18", "2021-09-14 16:08:32", "2021-11-11 22:57:30", "2021-10-27 22:12:47", "2021-10-13 09:37:12", "2021-09-16 17:44:01", "2021-11-05 13:49:26", "2021-11-24 14:26:09", "2021-09-19 07:32:12", "2021-09-18 17:15:08", "2021-11-26 18:42:34", "2021-10-27 22:00:47", "2021-11-08 15:12:09", "2021-09-23 11:31:41", "2021-09-15 16:35:11", "2021-09-23 16:23:28", "2021-11-11 07:37:07", "2021-09-17 05:06:50", "2021-09-28 10:21:24", "2021-12-20 22:42:36", "2021-12-01 15:18:58", "2021-11-12 14:37:26", "2021-09-28 08:12:09", "2021-11-14 12:44:34", "2021-10-28 10:27:34", "2021-09-16 15:39:52", "2021-09-24 10:49:48", "2021-10-10 10:22:11", "2021-10-27 08:33:05", "2021-09-23 17:06:40", "2021-11-14 09:34:59", "2021-09-21 20:42:07", "2021-11-10 13:33:36", "2021-11-08 20:01:49", "2021-09-16 08:13:41", "2021-10-27 19:34:19", "2021-09-12 06:12:06", "2021-11-30 16:25:39", "2021-09-16 21:01:28", "2021-12-16 11:34:06", "2021-11-17 10:42:11", "2021-09-16 07:43:16", "2021-12-13 14:45:38", "2021-09-24 11:17:00", "2021-09-22 12:58:53", "2021-11-03 17:45:11", "2021-11-08 17:21:37", "2021-12-02 16:51:48", "2021-09-23 16:41:39", "2021-12-03 12:36:04", "2021-09-22 20:28:32", "2021-11-22 23:35:40", "2021-12-20 21:51:17", "2021-11-05 17:55:12", "2021-11-09 16:53:42", "2021-12-22 16:31:00", "2022-01-08 06:33:11", "2022-01-08 06:33:11", ]; const allsaturadys = availabledates.map(x => new Date(x)).filter(x => x.getDay() === 6) console.log(allsaturadys)

Please note that Date manipulations may require one to consider timezones; however, this answer below does not take this crucial aspect into account.

const cd7 = new Date(); // current date minus 7 days
cd7.setDate(cd7.getDate() - 7);
const allsaturdays = availabledates
.map(x => new Date(x))
.filter(x => x.getDay() === 6 && x > cd7);
console.log(allsaturdays);

Explanation

  • Calculate the date 7 days before current-date in 'cd7' (a constant).
  • Filter array only when element is greater than 'cd7'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM