天天看點

時間庫Moment.js ,擷取時間,設定時間,格式化時間,比較時間

下載下傳位址:http://momentjs.cn/

擷取時間

  • Start of Time

    moment().startOf(String)
               
    • 擷取今天0時0分0秒
      moment().startOf('day')
                 
    • 擷取本周第一天(周日)0時0分0秒
      moment().startOf('week')
                 
    • 擷取本周周一0時0分0秒
      moment().startOf('isoWeek')
                 
    • 擷取目前月第一天0時0分0秒
      moment().startOf('month')
                 
  • End of Time

    moment().endOf(String)
               
    • 擷取今天23時59分59秒
      moment().endOf('day')
                 
    • 擷取本周最後一天(周六)23時59分59秒
      moment().endOf('week')
                 
    • 擷取本周周日23時59分59秒
      moment().endOf('isoWeek')
                 
    • 擷取目前月最後一天23時59分59秒
      moment().endOf('month')
                 
  • Days in Month

    moment().daysInMonth()
               
    • 擷取目前月的總天數
      moment().daysInMonth() 
                 
  • Timestamp

    • 擷取時間戳(以秒為機關)
    moment().format('X') // 傳回值為字元串類型
    moment().unix() // 傳回值為數值型
               
    • 擷取時間戳(以毫秒為機關)
    moment().format('x') // 傳回值為字元串類型
    moment().valueOf() // 傳回值為數值型
               
  • Get Time

    • 擷取年份
      moment().year()
      moment().get('year')
                 
    • 擷取月份
      moment().month() (0~11, 0: January, 11: December)
      moment().get('month')
                 
    • 擷取一個月中的某一天
      moment().date()
      moment().get('date')
                 
    • 擷取一個星期中的某一天
      moment().day() (0~6, 0: Sunday, 6: Saturday)
      moment().weekday() (0~6, 0: Sunday, 6: Saturday)
      moment().isoWeekday() (1~7, 1: Monday, 7: Sunday)
      moment().get('day')
      mment().get('weekday')
      moment().get('isoWeekday')
                 
    • 擷取小時
      moment().hours()
      moment().get('hours')
                 
    • 擷取分鐘
      moment().minutes()
      moment().get('minutes')
                 
    • 擷取秒數
      moment().seconds()
      moment().get('seconds')
                 
    • 擷取目前的年月日時分秒
      moment().toArray() // [years, months, date, hours, minutes, seconds, milliseconds]
      moment().toObject() // {years: xxxx, months: x, date: xx ...}
                 

設定時間

  • Set Time

    moment().year(Number), moment().month(Number)...
    moment().set(String, Int)
    moment().set(Object)
               
    • 設定年份
      moment().year(2019)
      moment().set('year', 2019)
      moment().set({year: 2019})
                 
    • 設定月份
      moment().month(11) (0~11, 0: January, 11: December)
      moment().set('month', 11) 
                 
    • 設定某個月中的某一天
      moment().date(15)
      moment().set('date', 15)
                 
    • 設定某個星期中的某一天
      moment().weekday(0) // 設定日期為本周第一天(周日)
      moment().isoWeekday(1) // 設定日期為本周周一
      moment().set('weekday', 0)
      moment().set('isoWeekday', 1)
                 
    • 設定小時
      moment().hours(12)
      moment().set('hours', 12)
                 
    • 設定分鐘
      moment().minutes(30)
      moment().set('minutes', 30)
                 
    • 設定秒數
      moment().seconds(30)
      moment().set('seconds', 30)
                 
  • Add Time

    moment().add(Number, String)
    moment().add(Object)
               
    • moment().add(1, 'years')
      moment().add({years: 1})
                 
    • moment().add(1, 'months')
                 
    • 設定日期
      moment().add(1, 'days')
                 
    • 設定星期
      moment().add(1, 'weeks')
                 
    • moment().add(1, 'hours')
                 
    • moment().add(1, 'minutes')
                 
    • moment().add(1, 'seconds')
                 
  • Subtract Time

    moment().subtract(Number, String)
    moment().subtract(Object)
               
    • moment().subtract(1, 'years')
      moment().subtract({years: 1})
                 
    • moment().subtract(1, 'months')
                 
    • moment().subtract(1, 'days')
                 
    • moment().subtract(1, 'weeks')
                 
    • moment().subtract(1, 'hours')
                 
    • moment().subtract(1, 'minutes')
                 
    • moment().subtract(1, 'seconds')
                 

格式化時間

  • Format Time

    moment().format()
    moment().format(String)
               
    • 格式化年月日: 'xxxx年xx月xx日'
      moment().format('YYYY年MM月DD日')
                 
    • 格式化年月日: 'xxxx-xx-xx'
      moment().format('YYYY-MM-DD')
                 
    • 格式化時分秒(24小時制): 'xx時xx分xx秒'
      moment().format('HH時mm分ss秒')
                 
    • 格式化時分秒(12小時制):'xx:xx:xx am/pm'
      moment().format('hh:mm:ss a')
                 
    • 格式化時間戳(以秒為機關)
      moment().format('X') // 傳回值為字元串類型
                 
    • 格式化時間戳(以毫秒為機關)
      moment().format('x') // 傳回值為字元串類型
                 

比較時間

  • Difference

    moment().diff(Moment|String|Number|Date|Array)
               
    • 擷取兩個日期之間的時間差
      let start_date = moment().subtract(1, 'weeks')
      let end_date = moment()
      
      end_date.diff(start_date) // 傳回毫秒數
      
      end_date.diff(start_date, 'months') // 0
      end_date.diff(start_date, 'weeks') // 1
      end_date.diff(start_date, 'days') // 7
      start_date.diff(end_date, 'days') // -7
                 

轉化為JavaScript原生Date對象

moment().toDate()
new Date(moment())
           
  • 将Moment時間轉換為JavaScript原生Date對象

    let m = moment()
    let nativeDate1 = m.toDate()
    let nativeDate2 = new Date(m)
    
    String(nativeDate1) === String(nativeDate2) // true
               

實戰

  • 擷取昨日0時0分0秒到昨日23時59分59秒, 格式:[milliseconds, milliseconds]
  • 擷取上周一到上周日時間範圍,格式: [seconds, seconds]
  • 擷取上個月第一天和最後一天時間範圍, 格式:[YYYY-MM-DD, YYYY-MM-DD]

給心靈一個純淨空間,讓思想,情感,飛揚!