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 .
Asked
Active
Viewed 3,072 times
-1
-
have you tried something ? – Dan M. CISSOKHO Oct 07 '16 at 12:35
-
yes i try many but can't get expected result. – Alex Kumbhani Oct 07 '16 at 12:35
-
http://stackoverflow.com/questions/11392186/how-to-check-a-date-is-within-current-week-or-current-month-or-next-month-in-jav – Deepak Oct 07 '16 at 12:37
-
2Can you post your code here? – abdulbarik Oct 07 '16 at 12:37
-
Show us your code? What have you tried? Have a look at MomentJs library – nagyf Oct 07 '16 at 12:41
-
1i try `var curr = new Date; var firstday = new Date(curr.setDate(curr.getDate() - curr.getDay())); var lastday=new Date((curr.setDate(curr.getDate() - curr.getDay()))+6); ` but i can not get last day – Alex Kumbhani Oct 07 '16 at 12:44
-
Thanks Dear All . i done it .it is very cool. look how i done it. – Alex Kumbhani Oct 07 '16 at 12:49
-
1`var curr = new Date; var firstday = new Date(curr.setDate(curr.getDate() - curr.getDay())); var totalAppo=response.data.appointment.length; for(i=0;i
=firstday) { console.log("date get",response.data.appointment[i]); } }` – Alex Kumbhani Oct 07 '16 at 12:50 -
You can give your own answer if it works for your... cheers!! – abdulbarik Oct 07 '16 at 12:55
4 Answers
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.
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