JS日期計算

一、工具函數(shù)

// 補0函數(shù),1補成01
function padding(num) {
    return num < 10 ? '0' + num : num
}
// 將日期格式化為2019/03/19
function formalDate(year, month, day) {
    return [year, padding(month), padding(day)].join('-')
}
// 日期格式化
function formalDate1(t) {
    var d = new Date(t);
    return [d.getFullYear(), padding(d.getMonth() + 1), padding(d.getDate())].join('-');
}

二、日期計算

1)根據(jù)給定年份計算周時間段列表:
function countWeekList(year) {
     var firstDay = new Date(year, 0, 1) // 獲取給定年份第一天的日期
     while (firstDay.getDay() != 1) { // 獲取給定年份第一個完整周的周一日期
        firstDay.setDate(firstDay.getDate() + 1)
     }
     // 獲取給定年份下一年的第一天的日期,用來計算給定年份最后一天的日期,省去計算平閏年的步驟
     var lastDay = new Date(year + 1, 0, 1)
     var i = 1  // 控制輸出
     var toDate, fromDate
     for (var from = firstDay; from < lastDay;) { 
         // 記錄第i個周一的日期
         fromDate = formalDate(year, from.getMonth() + 1, from.getDate())
         from.setDate(from.getDate() + 6) // +6獲取第i個周天的日期
         if (from < lastDay) { // 日期未超過下一年的第一天時
              toDate = formalDate(year, from.getMonth() + 1, from.getDate())
              from.setDate(from.getDate() + 1)
         } else { // 日期超過下一年的第一天時
              lastDay.setDate(lastDay.getDate() - 1) // 只截取當(dāng)前年份的日期,超出的部分不要
              toDate = formalDate(year, lastDay.getMonth() + 1, lastDay.getDate())
         }
         // 打印結(jié)果
         if (fromDate < toDate) document.write(year + '年第' + i + '周:' + fromDate + '至' + toDate + '<br / >')
            i++
     }
}
console.log(countWeekList(2018))
2)根據(jù)給定年份計算月時間段列表:
function countMonthList(year) {
     var monthList = {} // 保存月份時間段列表
     var beginDate = new Date(year, 0, 1) // 保存當(dāng)前年份第一天的日期
     var endDate = new Date(year + 1, 0, 1) // 保存當(dāng)前年份下一年第一天的日期
     // 根據(jù)endDate獲取一個完整月的時間段
     function getLast(et) {
         // 減去一天即為上月最后一天;
         et = et - 1000 * 60 * 60 * 24
         // 計算上月第一天的日期
         var t = new Date(et)
         t.setDate(1)
         st = t
         return {
              st: formalDate1(st),
              et: formalDate1(et)
         };
      }
      var i = 12
      while (i > 0) {
          monthList[i] = getLast(endDate)
          // 更新endDate的值
          endDate = new Date(monthList[i].st)
          i--
      }
      return monthList
}
console.log(countMonthList(2019))
3)根據(jù)給定年份計算當(dāng)前年份的周數(shù):
function countWeekNum(year) {
    var total = (year % 4 || year % 100) ? 365 : 366 // 計算當(dāng)前年份的總天數(shù)
    var first = (new Date(year, 0, 1, 0)).getDay() // 獲取當(dāng)前年份第一天是周幾
    var last = (new Date(year, 11, 31, 23)).getDay() // 計算當(dāng)前年份最后一天是周幾
    first = first === 0 ? 7 : first // getDay()返回日期為0~7,需重置以便后續(xù)計算
    // 如果最后一天是周日,只需減去first的相關(guān)值
    // 默認(rèn)從當(dāng)前年份的第一個周一開始計算,例如2019年1月1日是周二,總天數(shù)total需減去6
    if (last === 0) {
         return parseInt((total - (8 - first)) / 7)
    } else { 
         // 如果最后一天不是周日,總天數(shù)total需同時減去first和last的相關(guān)值,并在結(jié)果中加1
         // 2019年最后一周為2019-12-30-2020-12-31
         return parseInt((total - (8 - first) - last) / 7 + 1)
    }
 }
 console.log(countWeekNum2(2019)) // 52
4)根據(jù)當(dāng)前日期計算當(dāng)前月份的時間段:
// 根據(jù)當(dāng)前日期計算當(dāng)前月份的時間段:2019-02-01~2019-02-28
function countDayNum() {
    var curDate = new Date() // 獲取當(dāng)前日期
    // 獲取當(dāng)前月份最后一天的日期,setDate(0):設(shè)置為上個月最后一天
    var last = new Date(curDate.setMonth(curDate.getMonth() + 1))
    var last = new Date(curDate.setDate(0))
    var dayNum = last.getDate() // 獲取當(dāng)前月份的總天數(shù)
    var first = new Date(last.getFullYear(), last.getMonth(), 1) // 獲取當(dāng)前月份第一天的日期
    return [parseToPhpDate(first), parseToPhpDate(last), dayNum]
}
console.log(countDayNum2())
5)根據(jù)當(dāng)前日期計算當(dāng)前周的時間段:
// 根據(jù)當(dāng)前日期計算當(dāng)前周的時間段
// new Date(2019,1,29) === new Date(2019,2,1)
function countDayNum2() {
    var curDate = new Date()
    var day = curDate.getDay() === 0 ? 7 : curDate.getDay()
    var mondayTime = new Date(curDate.setDate(curDate.getDate() - (day - 1)))
    var sundayTime = new Date(mondayTime.getFullYear(), mondayTime.getMonth(), mondayTime.getDate() + 6)
    return [parseToPhpDate(mondayTime), parseToPhpDate(sundayTime)]
}
console.log(countDayNum2()) // ["2019-03-18", "2019-03-24"]
6)根據(jù)當(dāng)前日期獲取當(dāng)前是今年的第幾周
// 根據(jù)當(dāng)前日期獲取當(dāng)前是今年的第幾周
function countCurweeknum() {
    var curDate = new Date()
    var first = new Date(curDate.getFullYear(), 0, 1) // 獲取當(dāng)前年份第一天的日期
    while (first.getDay() !== 1) { // 獲取當(dāng)前年份第一個周一的日期
         first.setDate(first.getDate() + 1)
    }
    // 獲取當(dāng)前日期上一個周日的日期
    var last = new Date(curDate.setDate(curDate.getDate() - (curDate.getDay() === 0 ? 7 : curDate.getDay())))
    // 利用時間戳計算天數(shù)
    var weekNum = Math.ceil((last.getTime() - first.getTime()) / (7 * 1000 * 24 * 60 * 60)) + 1
    return weekNum
}
console.log(countCurweeknum()) // 11 2019-03-21
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容