Date對象經常使用的封裝方法及遇到的坑

使用JS中的Date對象已經好久了,可是一直以來都沒有將經常使用的封裝函數和遇到的坑記錄下來,趁着今天有空,就記錄下來,方便本身下次直接使用,並提醒本身遇到過那些坑。若是有哪些地方不對,但願你們可以指出,那我將不勝感激。

在將日期(沒有時分秒)轉換爲時間戳時,日期在用'-'(2019-01-01)和'/'(2019/01/01)鏈接時,轉換成時間戳的結果有所差別

爲了避免讓你們先看見例子太多而厭煩,就先上結論了。
 結論: 
     1)若是日期之間是使用 '-'鏈接時,當月份和天數都小於9且前面加了一個 0 的話,那麼被轉爲時間戳時會將時間默認轉換爲當天的上午8點
     2)若是日期之間是使用 '-'鏈接時,當月份和天數有一個小於9且小於9的前面加了一個 0 的話,那麼被轉爲時間戳時會將時間默認轉換爲當天的上午8點
     3)若是日期之間是使用 '-'鏈接時,當月份和天數都小於9且只有一個前面加了一個 0 的話,那麼被轉爲時間戳時會將時間默認轉換爲當天的上午12點也就是00:00
     4)若是日期之間是使用 '-'鏈接時,當月份和天數都大於9時,那麼被轉爲時間戳時會將時間默認轉換爲當天的上午8點
     5)若是日期之間是使用 '/'鏈接時,那麼轉換成時間戳時只會被轉換爲當天的凌晨 00:00
  總結: 在將日期轉換爲時間戳時,若是沒有設置時分時,最好使用 '/' 來進行鏈接,來避免相同日期在寫法不一樣時獲取的時間戳不一樣 
  下面的是用來證實結論的例子:
  ```
    let time1 = new Date('2019-03-03').getTime();
    let time2 = new Date('2019/3/3').getTime();
    console.log('獲取時間')
    console.log(time1)        
    console.log(time2) 
    console.log( (time1-time2) / 1000 /60 /60 )       // 8
    // 根據本地格式,把Date對象的時間轉換爲字符串  上午12:00:00也就是 00:00:00
    console.log(new Date('2019-03-03').toLocaleString())  // 2019/3/3 上午8:00:00
    console.log(new Date('2019-03-12').toLocaleString())  // 2019/3/12 上午8:00:00
    console.log(new Date('2019-11-03').toLocaleString())  // 2019/11/3 上午8:00:00
    console.log(new Date('2019-3-03').toLocaleString())  // 2019/3/3 上午12:00:00
    console.log(new Date('2019-03-3').toLocaleString())  // 2019/3/3 上午12:00:00
    console.log(new Date('2019-11-13').toLocaleString()) // 2019/11/13 上午8:00:00
    console.log(new Date('2019/03/03').toLocaleString())  // 2019/3/3 上午12:00:00
    console.log(new Date('2019/3/3').toLocaleString())  // 2019/3/3 上午12:00:00
    console.log(new Date('2019/03/3').toLocaleString())  // 2019/3/3 上午12:00:00
    console.log(new Date('2019/3/03').toLocaleString())  // 2019/3/3 上午12:00:00
    console.log(new Date('2019/03/12').toLocaleString())  // 2019/3/12 上午12:00:00
    console.log(new Date('2019/11/03').toLocaleString())  // 2019/11/3 上午12:00:00
  ```

將日期格式轉換爲時間戳和將時間戳轉換爲日期格式

1. 將日期格式轉換爲時間戳的三種方法
   ```javascript
    let dateStr = new Date('2019-3-20 18:59:39:123');
    let timestamp1 = dateStr.getTime();    // 1553079579123
    let timestamp2 = dateStr.valueOf();    // 1553079579123
    let timestamp3 = Date.parse(dateStr);  // 1553079579000
   ```
date.getTime()、date.valueOf()會精確到毫秒,而Date.parse(date)只能精確到秒,毫秒用000替代
2. 將時間戳轉換爲日期格式
    ```javascript
    function dateFormat(timestamp) {
        timestamp = (timestamp + '' ).length > 10 ? timestamp : timestamp * 1000;  //判斷時間戳爲幾位,10位時加上毫秒,13爲的則無論
        let date = new Date(timestamp);
        let year = date.getFullYear();
        let month = date.getMonth() + 1 > 9 ? date.getMonth() + 1 : '0' + (date.getMonth() + 1);   // 月份從0開始,0~11, 因此顯示時要 +1
        let day = date.getDate() > 9 ? date.getDate() : '0' + date.getDate() ;
        let hour = date.getHours() > 9 ? date.getHours() : '0' + date.getHours() ;
        let minutes = date.getMinutes() > 9 ? date.getMinutes() : '0' + date.getMinutes();
        let seconds = date.getSeconds() > 9 ? date.getSeconds() : '0' + date.getSeconds();
        return year + '-' + month + '-' + day + ' ' + hour + ':' +  minutes + ':' + seconds;
    }
    ```

比較兩個日期之間相隔多少天

/**
   * @method  計算兩個日期之間有幾天,包括第一天
   * @param  beginTime  開始時間的日期 '2019-3-19' || '2019/3/19'
   * @param  endTime    結束時間的日期 '2019-3-20' || '2019/3/19'
   */
getIntervalDay('2019-03-03', '2019-03-8');  // 如果沒有用 正則將格式轉換的話獲得的結果是5天,轉換後是6天
function getIntervalDay(beginTime, endTime) {
    // 先利用將其轉換爲統一的格式,由於 '-' 格式下的時間戳轉換的結果不一致,緣由在本文的開頭
    beginTime = beginTime.replace(/\-/g, '/');
    endTime = endTime.replace(/\-/g, '/');
    let time1 = new Date(beginTime).getTime();
    let time2 = new Date(endTime).getTime();
    // console.log(beginTime)
    // console.log(endTime)
    let second = time2 - time1;
    let day = parseInt(second / (1000 * 60 * 60 * 24)) + 1; // 當天也算進去
    return day;
}

判斷一年有多少天

// 閏年爲366天(2月中多一天),平年爲365天。
   // 閏年有兩種: 1)普通閏年:能被4整除但不能被100整除的年份爲普通閏年。
   //            2)世紀閏年:能被400整除的爲世紀閏年。
   function getYearAllDay(year) {
       return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0 ? 366 : 365;
   }

獲取某年的一個月的總天數

// date格式爲 'xxxx-xx-xx' 'xxxx/xx/xx' 'xxxx/xx' 'xxxx-xx'  
  function getMonthAllDay(date) {
      date = new Date(date);
      let year = date.getFullYear();
      let month = date.getMonth() + 1; // 從 Date 對象返回月份 (0 ~ 11)。 
      let nextMonth = year + '-' + (month + 1);  
      let newDate = new Date(nextMonth);
      newDate.setDate(0); // 利用設置日期時從1~31設置,當設置爲0時,即上個月的最後一天
      return newDate.getDate();
  }

API參考: http://www.w3school.com.cn/js...javascript

相關文章
相關標籤/搜索