检查给定时刻是否在同一周内,而不是一组日期字符串中的至少一个日期字符串

Check if a given moment lies in same week than at least one datestring out of an array of datestrings

所以在我的 backbone-app copyCLDRView 中,我尝试 copy/paste 周(以及底层 components/data)。或者抽象一点,"copy one week and the models in the week into another week".

现在我通常想检查目标周是否至少有一天(从星期一到星期日的目标周)至少有一个组件。要检查目标周中是否有组件,我有以下日期字符串数组,其中包含所有具有组件的日期:

        debug("[copyCLDRView::initialize] -> allDaysWithComponents: ", this.allDaysWithComponents);

例如可以包含以下值(格式为 DDMMYYYY 的日期字符串):

[copyCLDRView::initialize] -> allDaysWithComponents: ["20042015", "21042015", "22042015", "23042015", "24042015", "27042015", "28042015", "29042015", "30042015" ", "01052015", "11052015", "12052015", "13052015", "14052015", "15052015", "18052015", "19052015", "20052015", "21052015", "22052015", "25052015", "26052015", "27052015", "28052015", "29052015", "01062015", "02062015", "03062015", "04062015", "05062015"]

现在我必须检查此数组中的至少一个逻辑日期是否与给定的 Moment.js-对象(进一步称为 "moment")在同一周内,我设法做到了永远是星期一。

paste: function (evt) {
        //this.selected is my momentobject, e.g. Mon May 18 2015 00:00:00 GMT+0200
        if (this.selected && this.selected !== null) {
            //Here I need the check,
            //Pseudocode: if weekHasComponent(this.selected, alldaysWIthComponents) ...
            this.pasteData(this.selected, this.tmpStorage);
        }
    },

所以在这个例子中,我选择了从 2015 年 5 月 18 日开始的一周,现在我希望我的支票到 return "true" 至少有一个组件星期几 => 查看上面的内容,如果数组包含 至少一个 以下值,它应该 return 为真:1805201519052015200520152105201522052015

所以我在问自己 moment.js 是否可以帮助我进行此比较,但我还没有找到任何东西,希望你能提供帮助。

此致, 多米尼克

从各种日期字符串中创建时刻对象,然后在对 Array.prototype.some 的调用中使用 isSame 比较它们(或者,您可以使用 underscore.js 或 lodash)

var dates = ["20150101", "20150201"]
var testDate = moment("20150102", "YYYYMMDD")
dates.some(function(date){
  return moment(date, "YYYYMMDD").isSame(testDate,"week")
})
//Should return true for this first value of testDate

testDate = moment("20150115", "YYYYMMDD");
dates.some(function(date){
  return moment(date, "YYYYMMDD").isSame(testDate,"week")
})
//Should return false for this