如何在jQuery中计算一年中的总周数? [英] How to calculate the total weeks in a year in jQuery?

查看:430
本文介绍了如何在jQuery中计算一年中的总周数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用HTML打印星期,年,即43,2013.同样,当我按下prev按钮时,它应该打印42,2013,在按下下一步时,我应该得到44,2013.

I want to print the week,year in HTML, ie 43,2013. Also when I press prev button it should print 42,2013 and on pressing next I should get 44,2013.

我能够做到一个月,一年,即October,2013,并获得下一个先前的月份,但是一周无法做到.

I'm able to do it for month,year ie October,2013 and getting next presvious months but unable to do it for week.

这是我一个月的代码.请帮我编写一周的代码.

Here is my code for month. Kindly help me in writing the code for week.

function monthNav(nav) {

  if(nav == 'prev') {

    var newMonth = cur_dat.getMonth() - 1;
    var newYear = cur_dat.getFullYear();
    if(newMonth < 0) {
      newMonth = 11;
      newYear -= 1;
    }

    cur_dat.setMonth(newMonth);
    cur_dat.setFullYear(newYear);
    curMonthYear = month[newMonth] + ", " + newYear;
    $("#mNav").html(curMonthYear);


  } else {

    var newMonth = cur_dat.getMonth() + 1;
    var newYear = cur_dat.getFullYear();
    if(newMonth > 11) {
      newMonth = 0;
      newYear += 1;
    }

    if(newYear == curYear && newMonth > curMonth) {
      return false;
    }
    cur_dat.setMonth(newMonth);
    cur_dat.setFullYear(newYear);
    curMonthYear = month[newMonth] + ", " + newYear;
    document.getElementById('periodVal').value=curMonthYear;
    $("#mNav").html(curMonthYear);

  }

}

推荐答案

尝试一下-

function getWeeks(d) {
 var first = new Date(d.getFullYear(),0,1); //start of the year
 var dayms = 1000 * 60 * 60 * 24;
 var numday = ((d - first)/dayms)
 var weeks = Math.ceil((numday + first.getDay()+1) / 7) ; 
 return weeks
}
alert(getWeeks(new Date("1 Dec 2012"))); //end of the year     

并在此处检查您的代码 http://jsbin.com/ugesex/29/edit

and check your code here http://jsbin.com/ugesex/29/edit

这篇关于如何在jQuery中计算一年中的总周数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆