简体   繁体   English

如何在 javascript 中从本月过滤本周

[英]How to filter this week from this month in javascript

I have two functions one finds this week's files and the other one finds this month's files.我有两个函数,一个查找本周的文件,另一个查找本月的文件。 I want to extract/filter this week's files from this month's files.我想从本月的文件中提取/过滤本周的文件。

filterThisWeek(files) {
      const firstDayOfWeek = new Date(new Date().setDate(new Date().getDate() - new Date().getDay()));
      return this.filterToday(files).filter((f) => {
        return f.date >= firstDayOfWeek;
      });
    },
filterThisMonth(files) {
      const today = new Date();
      return files.filter((f) => {
        return (
          new Date(f.date).getMonth() === today.getMonth() &&
          new Date(f.date).getFullYear() === today.getFullYear()
        );
      });

using filterThisWeek function I want to extract this week's files from this month.使用 filterThisWeek function 我想从本月提取本周的文件。 So filterThisMonth function should find this month's files except this week's files.所以 filterThisMonth function 应该找到这个月的文件,除了本周的文件。 I want to do it in most efficient way but I dont know how to do it.我想以最有效的方式做到这一点,但我不知道该怎么做。

I'd suggest creating functions getStartOfWeek() and getStartOfMonth() to establish the required date thresholds.我建议创建函数getStartOfWeek()getStartOfMonth()来建立所需的日期阈值。

We'd then use these to create the desired limits in the filter functions filterThisWeek() and filterThisMonth() .然后,我们将使用它们在过滤器函数filterThisWeek()filterThisMonth()中创建所需的限制。

filterThisWeek() should return any files with a date greater than or equal to the start of the week while filterThisMonth() should return any files with a date greater than or equal to the start of the month and less then the start of the week; filterThisWeek() 应返回日期大于或等于一周开始的任何文件,而 filterThisMonth() 应返回日期大于或等于月初且小于一周开始的任何文件;

 function getStartOfWeek(date) { const dt = new Date(date.getFullYear(), date.getMonth(), date.getDate()); const offset = dt.getDate() - (dt.getDay() === 0? 6: dt.getDay() - 1); return new Date(dt.setDate(offset)); } function getStartOfMonth(date) { return new Date(date.getFullYear(), date.getMonth(), 1) } function filterThisWeek(files, referenceDate = new Date()) { const lowerThreshold = getStartOfWeek(referenceDate); return files.filter(file => file.date >= lowerThreshold); } function filterThisMonth(files, referenceDate = new Date()) { const lowerThreshold = getStartOfMonth(referenceDate); const upperThreshold = getStartOfWeek(referenceDate); return files.filter(file => file.date >= lowerThreshold && file.date < upperThreshold); } function formatFile(file) { return `${file.date.toLocaleDateString('sv')}`; } const testFiles = Array.from( { length: 14 }, (v, k) => { return { date: new Date(Date.now() - k*86400*1000)}; }) console.log('This weeks files:', filterThisWeek(testFiles).map(formatFile)); console.log('This months files:', filterThisMonth(testFiles).map(formatFile));
 .as-console-wrapper { max-height: 100%;important; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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