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

so in my backbone-app copyCLDRView I try to copypaste weeks (and the underlying ponentsdata). Or to b

so in my backbone-app copyCLDRView I try to copy/paste weeks (and the underlying ponents/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 ponent on at least one day (targetweek from monday to sunday). To have a check if there are ponents in the targetweek, I have the following array of datestrings which contains all dates that have ponents:

        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 ponent 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 parison here, but I didn't find something yet and hope you could help.

Best regards, Dominik

so in my backbone-app copyCLDRView I try to copy/paste weeks (and the underlying ponents/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 ponent on at least one day (targetweek from monday to sunday). To have a check if there are ponents in the targetweek, I have the following array of datestrings which contains all dates that have ponents:

        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 ponent 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 parison here, but I didn't find something yet and hope you could help.

Best regards, Dominik

Share Improve this question edited Apr 16, 2015 at 13:52 Dominik asked Apr 16, 2015 at 13:16 DominikDominik 2,9062 gold badges36 silver badges47 bronze badges 3
  • Use Array.some(). – Etheryte Commented Apr 16, 2015 at 13:20
  • 1 you can use moment to call week on it to check if two days are in the same week of the year, then pare year, something like . moment(date).week() === moment(date2).week() && moment(date).year() === moment(date2).year(). make sure you set correct local for the week so moment know it should start at Monday, default for US is Sunday. – Abraham Adam Commented Apr 16, 2015 at 13:29
  • @Nit Sorry, seems I'm not getting it, especially the built-in rangechecking. I just looked again into the docs :/ – Dominik Commented Apr 16, 2015 at 13:45
Add a ment  | 

1 Answer 1

Reset to default 7

Create moment objects out of your various date strings, then pare 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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744136749a4560093.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信