当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JS获取今天是本月第几周、本月共几周、本月有多少天、是今年的第几周、是今年的第几天的示例代码

JS获取今天是本月第几周、本月共几周、本月有多少天、是今年的第几周、是今年的第几天的示例代码

2019年01月04日  | 移动技术网IT编程  | 我要评论

好久没有记录工作中遇到的问题,其中的原因之一应该是没有什么代表性的或者说是没有网上搜不到答案的,毕竟在大多数前端中我还是很渺小。今天写这个博客就是因为工作中遇到了问题而且网上也没有找到合适的答案,自己写了大部分代码加上借鉴了一些别人的思想,下面就步入正题:

—更新:2018-6-20 加上今天是不是周日的判断

—更新:2018-7-31 给string添加方法来实现调用,感谢rainbow_miao的提醒。github地址:https://github.com/zancheng/weekcalculation

js源码

判断规则

第一周 : 是这个月的新一周,且不在上个月最后一周内。

// 获取某年某月的有多少周
string.prototype.weekinmonthcount = function () {
  var date = new date((new date(this).setdate(1)) || (new date()).setdate(1));
  var firstweekdate = 1;// 默认第一周是本月1号 为了模拟本月1号是否为本月第1周的判断
  if (date.getday() === 1) { // 判断1号是周一
    firstweekdatek = 1;
  } else if (date.getday() === 0) { // 判断1号是周日
    firstweekdate = 8 - 7 + 1;
  } else { // 判断1号是周二至周六之间
    firstweekdate = 8 - date.getday() + 1;
  }
  date.setmonth(date.getmonth()+1);
  date.setdate(0);
  var monthhasdays = date.getdate();// 本月天数
  monthhasdays = date.getdate() - firstweekdate + 1;
  var hasweek = math.ceil(monthhasdays/7); // 计算本月有几周
  return hasweek;
};
// 获取今天是今年的第几周
string.prototype.weekindexinyear = function () {
  var nowdate = new date(this != '' ? this : new date());
  var inittime = new date(this != '' ? this : new date());
  inittime.setmonth(0); // 本年初始月份
  inittime.setdate(1); // 本年初始时间
  var differenceval = nowdate - inittime ; // 今天的时间减去本年开始时间,获得相差的时间
  var todayyear = math.ceil(differenceval/(24*60*60*1000)); // 获取今天是今年第几天
  var index = math.ceil(todayyear/7); // 获取今天是今年第几周
  return index;
};
// 获取今天是今年的第几天
string.prototype.dateindexinyear = function () {
  var nowdate = new date(this != '' ? this : new date());
  var inittime = new date(this != '' ? this : new date());
  inittime.setmonth(0); // 本年初始月份
  inittime.setdate(1); // 本年初始时间
  var differenceval = nowdate - inittime ; // 今天的时间减去本年开始时间,获得相差的时间
  return math.ceil(differenceval/(24*60*60*1000));
};
// 获取今天是第几周
string.prototype.weekindexinmonth = function () {
  var date = new date(this.trim() != '' ? this : new date());
  var datestart = new date((new date(this.trim() != '' ? this : new date()).setdate(1))); // 本月初
  var firstweek = 1;
  if (datestart.getday() === 1) {
    firstweek = 1;
  } else if (datestart.getday() === 0) {
    firstweek = 8 - 7 + 1;
  } else {
    firstweek = 8 - datestart.getday() + 1;
  }
  var weekindex = 1;
  var c = date.getdate();
  if (date.getday() === 1 && date.getdate() < 7) {
    weekindex = 1;
  } else if (c < firstweek ) {
    weekindex = -1;
  } else {
    if (c < 7) {
      weekindex = math.ceil(c/7);
    } else {
      c = c - firstweek + 1;
      if (c%7 === 0) {
        if (datestart.getday() !== 6) {
          weekindex = c/7;
        } else {
          weekindex = c/7 + 1;
        }
      } else {
        weekindex = math.ceil(c/7);
      }
    }
  }
  return weekindex;
};

方法说明及调用示例

string.prototype.dateindexinyear

获取这一天属于今年的第多少天

默认时间是今天,调用方法示例:

'2018/10/1'.dateindexinyear()
返回: 273

string.prototype.weekindexinyear

获取这一天属于今年的第多少周

默认时间是今天,调用方法示例:

'2018-10-1'.weekindexinyear()
返回: 39

string.prototype.weekinmonthcount

获取这一年的这一月的有多少周

默认时间是今天,调用方法示例:

'2018-10-1'.weekinmonthcount()

返回: 5

string.prototype.weekindexinmonth

获取这一周属于本月第多少周

如果属于上个月,返回 -1

默认时间是今天,调用方法示例:

'2018-10-01'.weekindexinmonth()
返回: 1

总结

以上所述是小编给大家介绍的js获取今天是本月第几周、本月共几周、本月有多少天、是今年的第几周、是今年的第几天,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网