溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》
  • 首頁 > 
  • 教程 > 
  • 開發技術 > 
  • web開發 > 
  • JS獲取今天是本月第幾周、本月共幾周、本月有多少天、是今年的第幾周、是今年的第幾天的示例代碼

JS獲取今天是本月第幾周、本月共幾周、本月有多少天、是今年的第幾周、是今年的第幾天的示例代碼

發布時間:2020-09-12 05:11:17 來源:腳本之家 閱讀:901 作者: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

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女