How Can I check selected date is in current week or not in JavaScript?

被刻印的时光 ゝ 提交于 2021-02-08 12:01:22

问题


I have one problem in JavaScript that I got many dates from a database and I want to get the date which is in the current week .


回答1:


Okay, I googled for you:

Get week of year in JavaScript like in PHP

Use this function and compare values. But probably it is possible to write more optimal funciton for your purposes.




回答2:


Try to use mement.js method isBetween to check the date which exists in week or not

  if(moment('2010-10-20').isBetween('2010-10-19', '2010-10-25')){
     //return true if exists
    //Do whatever you want
  }



回答3:


You could use moment().week() from momentjs.

Get the current week like this

var c = moment().week();

Now to get the week of any date

var pastWeek = moment('2016-03-21 20:05:32').week();

Compare these two.

P.S : While you explore moment().week(), also explore moment().isoWeek()




回答4:


I got my solution by doing this

    //get first date of current week
var curr = new Date;
var firstday = new Date(curr.setDate(curr.getDate() - curr.getDay()));

//cout how much date are in database and use for loop for compare all date
var totalAppo = response.data.appointment.length;
var dbdate = new Date(response.data.appointment[i].schedule_date);

for (i = 0; i < totalAppo; i++) {
  if (dbdate >= firstday) {
    //this date is in current week
  } else {
    //this date in outer of current week
  }
}

this way we can get data from database which is in current week



来源:https://stackoverflow.com/questions/39917439/how-can-i-check-selected-date-is-in-current-week-or-not-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!