javascript 時間轉(zhuǎn)換整理

  /**
   * 生成具體日期時間
   * @param timestamp 時間戳
   * @type yyyy-mm-dd hh:mm:ss
   */
  createTime(timestamp){ 
    var date = timestamp ? new Date(timestamp) : new Date();//實例一個時間對象,timestamp無則是當前時間;
    var year = date.getFullYear();//獲取系統(tǒng)的年;
    var month = date.getMonth() + 1;//獲取系統(tǒng)月份,由于月份是從0開始計算,所以要加1
    var day = date.getDate(); //獲取系統(tǒng)日
    var hour = date.getHours();//獲取系統(tǒng)時間
    var minute = date.getMinutes(); //分
    var second = date.getSeconds();//秒
    return year + '-' + this.formateDateDounle(month) + '-' + this.formateDateDounle(day) + ' ' + this.formateDateDounle(hour) + ':' + this.formateDateDounle(minute) + ':' + this.formateDateDounle(second)
  },
  /**
   * 獲取日期(年月日)
   * @param dateDesc
   * @type yyyy-mm-dd
   */
  getDay(timestamp){
    var date = timestamp ? new Date(timestamp) : new Date()
    var year = date.getFullYear();//獲取系統(tǒng)的年;
    var month = date.getMonth() + 1;//獲取系統(tǒng)月份,由于月份是從0開始計算,所以要加1
    var day = date.getDate(); //獲取系統(tǒng)日
    return year + '-' + this.formateDateDounle(month) + '-' + this.formateDateDounle(day)
  },
  /**
   * 獲取后幾個小時時間
   * @param num 后幾小時
   * @param timestamp 時間戳
   * @type yyyy-mm-dd hh:mm:ss
   */
  getLateTime(num, timestamp){
    if (!timestamp) { //不傳則為當前時間延遲
      timestamp = new Date().getTime()
    }
    return this.createTime(timestamp + num * 60 * 60 * 1000)
  },
  /**
   * 時間轉(zhuǎn)換兩位數(shù)字
   * @param num
   */
  formateDateDounle(num){
    return num.toString().length > 1 ? num : '0' + num
  },
  /**
   * 獲取時分
   * @param date 格式 yyyy-mm-dd hh:mm:ss
   * @type hh:mm
   */
  getHourMin(date){
    let dateArr = this.getLateTime(1).split(' ')[1].split(':')
    return dateArr[0] + ':' + dateArr[1];
  }

?著作權(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)容