0

This is my code

const date = new Date(); const startDate = new Date(date.getFullYear(), date.getMonth(), 1); const endDate = new Date(date.getFullYear(), date.getMonth() + 1, 0); const getDateArray = function(start, end) { const arr = []; const dt = new Date(start); while (dt <= end) { arr.push(new Date(dt)); dt.setDate(dt.getDate() + 1); } return arr; } const dateArr = getDateArray(startDate, endDate); 

in this above code I got the current month date list dateArr, Now I need to group the days by a week, from the week list I need to filter only week start date and weekend date that must be in list formate I tried with the above code but I cant proceed to next.

4
  • 1
    what you need specifically. Please mention that. How do you want weeks data Commented Jan 5, 2018 at 7:16
  • stackoverflow.com/questions/2483719/… refer this like Commented Jan 5, 2018 at 7:23
  • Possible duplicate of Get Weeks In Month Through Javascript Commented Jan 5, 2018 at 7:26
  • @stack26 i had rewrite the code can u check and tell me Commented Jan 5, 2018 at 7:51

1 Answer 1

1

This works.

var date = new Date(); console.log("All Weeks : ", getWeeksInaMonth()) console.log("Current Week : ", getCurrentWeek()) console.log("Current and previous weeks : ",getCurrAndPrevWeeks()) function getFormattedDate(dateobj) { var date = dateobj.getDate(), month = dateobj.getMonth()+1, year = dateobj.getFullYear(); var formattddate = (date<10?"0":"")+date+"/"+(month<10?"0":"")+month+"/"+year; return formattddate; } function getWeeksInaMonth() { var startdate = new Date(date.getFullYear(), date.getMonth(), 1); var enddate = new Date(date.getFullYear(), date.getMonth()+1, 0); var weeks = []; for(var i=1,n=enddate.getDate();i<n;) { startdate.setDate(i); var arr = [getFormattedDate(startdate)]; i =i+ 6-startdate.getDay(); if(i>n) i=i-(i-n); startdate.setDate(i); arr.push(getFormattedDate(startdate)); i++; weeks.push(arr); } return weeks } function getCurrentWeek() { var today = new Date(), day = today.getDay(); return [getFormattedDate(new Date(today.getFullYear(),today.getMonth(),today.getDate()-day)), getFormattedDate(new Date(today.getFullYear(),today.getMonth(),today.getDate()+6-day))]; } function getCurrAndPrevWeeks() { var startdate = new Date(date.getFullYear(), date.getMonth(), 1); var enddate = new Date(date.getFullYear(), date.getMonth()+1, 0); var today = new Date().getDate(); var weeks = []; for(var i=1,n=enddate.getDate();i<n;) { startdate.setDate(i); var arr = [getFormattedDate(startdate)]; i =i+ 6-startdate.getDay(); if(i>n) i=i-(i-n); startdate.setDate(i); arr.push(getFormattedDate(startdate)); weeks.push(arr); if(today>=i-6 && today<=i) break; i++; } return weeks; }

Sign up to request clarification or add additional context in comments.

5 Comments

thank you for the reply. if it possible to show like this output {startDate:"01/01/2018", endDate:"06/01/2018" }
Modified the array type to object type as required. Replace the following lines : 2nd : var obj = {}; obj.startDate = getFormattedDate(startdate); 6th : obj.endDate = getFormattedDate(startdate); 8th : weeks.push(obj);
You mean "The present week" ?
is it possible to show prev week start date and end date, current week start date and end date only in the array. Example [ "01/01/2018", "07/01/2018"] ,[ "08/01/2018", "14/01/2018"]
Updated them. @AaruMugam

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.