前端研习社

JS 获取当前月份的第一天与当前月份的最后一天

2018-11-29  本文已影响4人  AR7_

获取当前月份的第一天

    getCurrentMonthFirstDay() {
      var date = new Date()
      date.setDate(1)
      var month = parseInt(date.getMonth() + 1)
      var day = date.getDate()
      if (month < 10) {
        month = '0' + month
      }
      if (day < 10) {
        day = '0' + day
      }
      return date.getFullYear() + '-' + month + '-' + day
    }

获取当前月份的最后一天

    getCurrentMonthLastDay() {
      var date = new Date()
      var year = date.getFullYear()
      var month = date.getMonth() + 1
      month = month < 10 ? '0' + month : month
      var day = new Date(year, month, 0)
      return  year + '-' + month + '-' + day.getDate()
    }

参考博文:
https://www.cnblogs.com/visi_zhangyang/p/3490122.html
https://blog.csdn.net/m0_37852904/article/details/83182173

获取现在时间

    getNowFormatDate() {
      var date = new Date()
      var seperator1 = '-'
      var seperator2 = ':'
      var month = date.getMonth() + 1
      var strDate = date.getDate()
      if (month >= 1 && month <= 9) {
        month = '0' + month
      }
      if (strDate >= 0 && strDate <= 9) {
        strDate = '0' + strDate
      }
      var currentdate =
        date.getFullYear() +
        seperator1 +
        month +
        seperator1 +
        strDate +
        ' ' +
        date.getHours() +
        seperator2 +
        date.getMinutes() +
        seperator2 +
        date.getSeconds()
      return currentdate
    }
上一篇下一篇

猜你喜欢

热点阅读