温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》
  • 首页 > 
  • 教程 > 
  • 开发技术 > 
  • web开发 > 
  • JS获取今天是本月第几周、本月共几周、本月有多少天、是今年的第几周、是今年的第几天的示例代码

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

发布时间:2020-09-12 05:11:17 来源:脚本之家 阅读:817 作者:CN-cheng 栏目:web开发

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

—更新: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获取今天是本月第几周、本月共几周、本月有多少天、是今年的第几周、是今年的第几天,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对亿速云网站的支持!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI