国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

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

2019-11-19 12:24:28
字體:
來源:轉載
供稿:網友

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

―更新: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獲取今天是本月第幾周、本月共幾周、本月有多少天、是今年的第幾周、是今年的第幾天,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 墨江| 民丰县| 勃利县| 稻城县| 大同县| 湄潭县| 潼南县| 华池县| 温州市| 夏邑县| 西吉县| 武邑县| 中宁县| 苍溪县| 来宾市| 武汉市| 饶河县| 长治市| 平昌县| 泰和县| 孟村| 陆良县| 无为县| 武邑县| 封丘县| 怀柔区| 台州市| 株洲县| 临高县| 美姑县| 江口县| 英山县| 汨罗市| 齐河县| 湖口县| 左权县| 荥阳市| 天等县| 兴文县| 囊谦县| 福鼎市|