Reputation: 2906
so in my backbone-app copyCLDRView I try to copy/paste weeks (and the underlying components/data). Or to be a little abstract, "copy one week and the models in the week into another week".
Now I generally want to implement a check if the target-week has at löeast one component on at least one day (targetweek from monday to sunday). To have a check if there are components in the targetweek, I have the following array of datestrings which contains all dates that have components:
debug("[copyCLDRView::initialize] -> allDaysWithComponents: ", this.allDaysWithComponents);
which for example could contain the following values (Datestrings in format 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"]
Now I have to check if at least one of the logical dates in this array is in the same week than a given Moment.js-object (further called "moment"), which I managed to always be a monday.
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);
}
},
So in this example I chose the selected week to be the one starting with 18th of May 2015, now I want my check to return "true" when there is a component in at least one of the days of the week => looking above, it should return true if the array contains at least one of the following values: 18052015
, 19052015
, 20052015
, 21052015
, 22052015
So I'm asking myself if moment.js could help me out with this comparison here, but I didn't find something yet and hope you could help.
Best regards, Dominik
Upvotes: 2
Views: 6995
Reputation: 439
Create moment objects out of your various date strings, then compare them using isSame
inside of a call to Array.prototype.some
(or alternatively, you can use underscore.js or 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
Upvotes: 7