计算当前日期是今年第几周的JS代码封装

来源:互联网 发布:域名抵押贷款骗局 编辑:程序博客网 时间:2024/05/21 17:03

最近身边的人有做一个项目, 然后里面有个小模块, 需要作出如下图所示的效果:

一个下拉框, 然后里面有显示日期已经今年的第几周的这样一个效果, 具体完整的流程我暂时还没想到, 但是找到了一个能够算出当前系统日期是今年的第几周的一个封装好的方法, 如下所示:

  1. functiontheWeek() {
  2.     var totalDays = 0;
  3.     now = new Date();
  4.     years = now.getYear()
  5.     if (years < 1000)
  6.         years += 1900
  7.     var days = new Array(12);
  8.     days[0] = 31;
  9.     days[2] = 31;
  10.     days[3] = 30;
  11.     days[4] = 31;
  12.     days[5] = 30;
  13.     days[6] = 31;
  14.     days[7] = 31;
  15.     days[8] = 30;
  16.     days[9] = 31;
  17.     days[10] = 30;
  18.     days[11] = 31;
  19.     
  20.     //判断是否为闰年,针对2月的天数进行计算
  21.     if (Math.round(now.getYear() / 4) == now.getYear() / 4) {
  22.         days[1] = 29
  23.     else {
  24.         days[1] = 28
  25.     }
  26.     if (now.getMonth() == 0) {
  27.         totalDays = totalDays + now.getDate();
  28.     else {
  29.         var curMonth = now.getMonth();
  30.         for (var count = 1; count <= curMonth; count++) {
  31.             totalDays = totalDays + days[count - 1];
  32.         }
  33.         totalDays = totalDays + now.getDate();
  34.     }
  35.     //得到第几周
  36.     var week = Math.round(totalDays / 7);
  37.     return week;
  38. }
0 0