$gte: ISODate("2010-04-29T00:00:00.000Z"),$lt: ISODate("2010-05-01T00:00:00.000Z")
Asked
Active
Viewed 265 times
0
-
1What have you tried? What didn't work? – jo_va Feb 01 '19 at 17:32
-
i tried to rung mongodb query to get data between last sunday and next sunday of the week. – Kelum Sampath Edirisinghe Feb 01 '19 at 17:34
-
Both requirements (first and last day of a week, date in iso format) are not new. You should be able to find enough resources to get you (at least) started. – Andreas Feb 01 '19 at 17:38
-
how can i do it? – Kelum Sampath Edirisinghe Feb 01 '19 at 17:43
-
@str this is in different format, i want it in ISO date format. – Kelum Sampath Edirisinghe Feb 01 '19 at 17:46
-
You don't need to care about the format at all. Just use the JavaScript date object, the MongoDB driver handles it correctly. – str Feb 01 '19 at 17:49
-
thnak you! it's work properly – Kelum Sampath Edirisinghe Feb 01 '19 at 18:23
1 Answers
0
You may get the day of the week by calling .getDay() on the date object. Once you have the day of the week, rest are arithmetic operations.
To convert Date to ISODateFormat you can use toISOString
var dateForCalculation = new Date();
var prevSunday = new Date(dateForCalculation.setDate(dateForCalculation.getDate()-dateForCalculation.getDay())).toISOString();
var nextSunday = new Date(dateForCalculation.setDate(dateForCalculation.getDate()+7)).toISOString();
console.log(prevSunday);
console.log(nextSunday);

Ramesh
- 13,043
- 3
- 52
- 88