最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2018年12月05日 08:47:17   作者:CN-cheng  
這篇文章主要介紹了JS獲取今天是本月第幾周、本月共幾周、本月有多少天、是今年的第幾周、是今年的第幾天,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下

好久沒有記錄工作中遇到的問題,其中的原因之一應該是沒有什么代表性的或者說是沒有網(wǎng)上搜不到答案的,畢竟在大多數(shù)前端中我還是很渺小。今天寫這個博客就是因為工作中遇到了問題而且網(wǎng)上也沒有找到合適的答案,自己寫了大部分代碼加上借鑒了一些別人的思想,下面就步入正題:

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

—更新:2018-7-31 給String添加方法來實現(xiàn)調(diào)用,感謝Rainbow_miao的提醒。github地址:https://github.com/zancheng/weekCalculation

JS源碼

判斷規(guī)則

第一周 : 是這個月的新一周,且不在上個月最后一周內(nèi)。

// 獲取某年某月的有多少周
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();// 本月天數(shù)
  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;
};

方法說明及調(diào)用示例

String.prototype.dateIndexInYear

獲取這一天屬于今年的第多少天

默認時間是今天,調(diào)用方法示例:

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

String.prototype.weekIndexInYear

獲取這一天屬于今年的第多少周

默認時間是今天,調(diào)用方法示例:

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

String.prototype.weekInMonthCount

獲取這一年的這一月的有多少周

默認時間是今天,調(diào)用方法示例:

'2018-10-1'.weekInMonthCount()

返回: 5

String.prototype.weekIndexInMonth

獲取這一周屬于本月第多少周

如果屬于上個月,返回 -1

默認時間是今天,調(diào)用方法示例:

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

總結

以上所述是小編給大家介紹的JS獲取今天是本月第幾周、本月共幾周、本月有多少天、是今年的第幾周、是今年的第幾天,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關文章

最新評論

洛隆县| 噶尔县| 兴国县| 青神县| 木兰县| 资中县| 彩票| 班戈县| 凤庆县| 沅陵县| 日喀则市| 深水埗区| 宜州市| 辛集市| 宣恩县| 杨浦区| 寿阳县| 漳州市| 上饶县| 石首市| 香格里拉县| 宁都县| 东港市| 卓资县| 安顺市| 苏尼特右旗| 重庆市| 昆山市| 中西区| 武义县| 星座| 金堂县| 肥西县| 得荣县| 阜新| 郯城县| 来凤县| 九龙坡区| 米泉市| 中方县| 邵东县|