Get Weeks In Month Through Javascript
Weeks start on Sunday
This ought to work even when February doesn't start on Sunday.
function weekCount(year, month_number) {
// month_number is in the range 1..12
var firstOfMonth = new Date(year, month_number-1, 1);
var lastOfMonth = new Date(year, month_number, 0);
var used = firstOfMonth.getDay() + lastOfMonth.getDate();
return Math.ceil( used / 7);
}
Weeks start on Monday
function weekCount(year, month_number) {
// month_number is in the range 1..12
var firstOfMonth = new Date(year, month_number-1, 1);
var lastOfMonth = new Date(year, month_number, 0);
var used = firstOfMonth.getDay() + 6 + lastOfMonth.getDate();
return Math.ceil( used / 7);
}
Weeks start another day
function weekCount(year, month_number, startDayOfWeek) {
// month_number is in the range 1..12
// Get the first day of week week day (0: Sunday, 1: Monday, ...)
var firstDayOfWeek = startDayOfWeek || 0;
var firstOfMonth = new Date(year, month_number-1, 1);
var lastOfMonth = new Date(year, month_number, 0);
var numberOfDaysInMonth = lastOfMonth.getDate();
var firstWeekDay = (firstOfMonth.getDay() - firstDayOfWeek + 7) % 7;
var used = firstWeekDay + numberOfDaysInMonth;
return Math.ceil( used / 7);
}
Getting current week of current month
The ISO week in month number is based on Mondays, so Thursday, 2 March 2017 is in week 4 of February 2017 and Monday 6 March 2017 is in week 1 of March.
So the algorithm is to move to the previous Monday and see which Monday of the month it is. I would have thought this was a duplicate but I can't find one, so here's a function.
/* Get ISO week in month, based on first Monday in month
** @param {Date} date - date to get week in month of
** @returns {Object} month: month that week is in
** week: week in month
*/
function getISOWeekInMonth(date) {
// Copy date so don't affect original
var d = new Date(+date);
if (isNaN(d)) return;
// Move to previous Monday
d.setDate(d.getDate() - d.getDay() + 1);
// Week number is ceil date/7
return {month: +d.getMonth()+1,
week: Math.ceil(d.getDate()/7)};
}
//*
[new Date(2017,2,2), // Thu 2 Mar 2017
new Date(2017,2,6), // Mon 6 Mar 2017
new Date(2017,4,31), // Wed 31 May 2017
new Date()].forEach( // Current date
function(date) {
console.log(date.toString() + '\nis in week ' +
getISOWeekInMonth(date).week + ' of month ' +
getISOWeekInMonth(date).month);
}
);
//*/
how to get weeks in particular month with dates in java script
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;}
Related Topics
Php - Return Confirm Within PHP Issue
415 (Unsupported Media Type) With Rest Post Request
How to Make Any Hovered Color Darker/Lighter That the Original
Reinitialize/Reset Dropzone After Form Is Submitted
Removing Currency Symbol and Replacing Comma With Point Using Pure JavaScript
Why Isnt Window.Location.Href= Not Forwarding to Page Using Safari
How to Access a Model Attribute With JavaScript Variable
Edit Table Row Inline on Click of Edit in Angular
How to Intercept Button Click Inside Uiwebview on Ios
How to Make Mongoose Not Insert Empty Array or Object Fields into a Document
Node Js - Function to Return Array of Objects Read from Sequelize Database
How to Change Options Based on Another Select Option in a Table
How to Check If Value Is in Array With Angularjs
Converting Json Object to CSV Format in JavaScript
Laravel Reactjs:Changes Are Not Reflecting After Change in My Js(Reactjs) File