获取当前时间和当前的未来几个月的时间

2020-07-02  本文已影响0人  w_小伍
获取当前时间
function getNowFormatDate() {
      let date = new Date()
      let seperator1 = "-"
      let seperator2 = ":"
      let month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1
      let strDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate()
      let currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
        + " " + date.getHours() + seperator2 + date.getMinutes()
        + seperator2 + date.getSeconds()
      return currentdate
    }
    console.log(getNowFormatDate())//2020-07-02 13:52:29
当前的未来几个月的时间
let data = function (mounth) {
      //创建现在的时间
      let data = new Date()
      //获取年
      let year = data.getFullYear()
      //获取月
      let mon = data.getMonth() + 1
      //获取日
      let dated = data.getDate()
      //小时
      let hours = data.getHours()
      //分钟
      let minute = data.getMinutes()
      //秒
      let second = data.getSeconds()
      let arry = new Array()
      for (let i = 0; i < mounth; i++) {
        mon = mon + 1
        if (mon > 12) {
          year = year + 1
          mon = mon - 12
        }
      //过去几个月
        /*mon = mon - 1
        if (mon <= 0) {
          year = year - 1
          mon = mon + 12
        }*/
        let dating = new Date(year, mon, 0).getDate()//每个月的最后一天
        let date = dated > dating ? dating : dated//每个月的最后一天不一样,有些30,有些31
        arry[i] = year + "/" + mon + "/" + date + "/" + hours + "/" + minute + "/" + second
      }
      return arry
    }
    console.log(data(2))// ["2020/8/2/13/49/1", "2020/9/2/13/49/1"]
上一篇下一篇

猜你喜欢

热点阅读