-1

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 .

abdulbarik
  • 6,101
  • 5
  • 38
  • 59
Alex Kumbhani
  • 125
  • 2
  • 11

4 Answers4

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.

Community
  • 1
  • 1
Alexey
  • 59
  • 7
0

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
  }
abdulbarik
  • 6,101
  • 5
  • 38
  • 59
0

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()

nirvair
  • 4,001
  • 10
  • 51
  • 85
0

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

abdulbarik
  • 6,101
  • 5
  • 38
  • 59
Alex Kumbhani
  • 125
  • 2
  • 11