0

i want to get total number of weeks from date

Get Weeks In Month Through Javascript

this was the post i referred for my problem.

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);
}

for above code i just found bug. week starts on monday. bt above code i think considers from sunday. so its giving error for june 2014 month. function returns 5 bt actual weeks are 6. can anyone help me ?

Community
  • 1
  • 1
Chintan_chiku
  • 425
  • 2
  • 12
  • 24
  • What is *your* definition of "one week?" In my calendar, June 2014 spans 5 weeks, not 6: http://i.stack.imgur.com/KaVty.png – Matt Ball Feb 26 '14 at 05:49
  • it should end on sunday. so now if month starts from Saturday like feb 2014 1st week ends on 2nd feb and so on. above code is working fine for all months except for months, starts from sunday. – Chintan_chiku Feb 26 '14 at 05:52
  • 1
    @MattBall absolutely wrong. current month is 6 weeks long in my calender. consider that, if 1st comes on a Saturday, it ocupies 1 week – Ganesh Jadhav Feb 26 '14 at 05:57
  • bec week starts from sunday just see if first day is monday in calendar and regenerate it @MattBall – Chintan_chiku Feb 26 '14 at 05:57
  • 1
    @P5Coder no, the current month (February 2014) **spans 5 weeks.** http://i.stack.imgur.com/x1QaJ.png – Matt Ball Feb 26 '14 at 05:58
  • @Chintan_chiku Check `alert(weekCount(2014,8));` for `August, 2014` which gives `6`. – Rohan Kumar Feb 26 '14 at 06:00
  • @RohanKumar check for june 2014 and note that week starts from monday than check – Chintan_chiku Feb 26 '14 at 06:01
  • 2
    @MattBall bt month can have 6 weeks thats the point. june month can be consider of month of 6 weeks if week starts from monday – Chintan_chiku Feb 26 '14 at 06:02
  • 1
    @Chintan_chiku what you want `missing Sunday` situation. Above code is working for valid weeks(including months starting from any day instead of monday), you want to get weeks starting from Monday, then you have to make your own logic. – Rohan Kumar Feb 26 '14 at 06:05
  • @P5Coder ...even if you consider a week to start on Monday: http://i.stack.imgur.com/CzvPL.png – Matt Ball Feb 26 '14 at 06:06
  • @MattBall - dude..." so its giving error for june 2014 month. function returns 5 bt actual weeks are 6." June. – bokkie Jun 05 '14 at 14:59

1 Answers1

1

The above code considers Monday as the first day of week. So, if you want to use Sunday as the first day of week, just move the firstDay ahead by 1.

var used = firstOfMonth.getDay() % 7 + 1 + lastOfMonth.getDate();

Fiddle here

var year = 2014;
var month_number = 3;
var firstOfMonth = new Date(year, month_number - 1, 1);
var lastOfMonth = new Date(year, month_number, 0);
var firstDay = firstOfMonth.getDay();
if (firstDay == 7)
  firstDay = 1;
else
  firstDay += 1;
var used = firstOfMonth.getDay() % 7 + 1 + lastOfMonth.getDate();
document.querySelector('div').innerHTML = Math.ceil(used / 7);
<div></div>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Ganesh Jadhav
  • 2,830
  • 1
  • 20
  • 32