简体   繁体   中英

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

我在 JavaScript 中有一个问题,我从数据库中获取了很多日期,我想获取当前周的日期。

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.

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
  }

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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