美文网首页
js时间戳转日期

js时间戳转日期

作者: jack_rofer | 来源:发表于2020-08-20 00:44 被阅读0次
    function getTime(days,flag){
       let current = +new Date()
       let difference = days*24*3600*1000;
       let result = 0;
       if(flag === 'front'){
         result = current - difference;
       }else if(flag === 'after'){
         result = current + difference;           
       }else{
         throw Error('缺少flag参数'); 
       }
       const data = new Date(result);
       let Y = data.getFullYear(),
           M = data.getMonth()+1 >9 ? data.getMonth()+1: '0'+ (data.getMonth()+1),
           D = data.getDate()>9 ? data.getDate(): '0'+ data.getDate(),
           h = data.getHours()>9? data.getHours(): '0' + data.getHours(),
           m = data.getMinutes()>9? data.getMinutes(): '0' + data.getMinutes(),
           s = data.getSeconds()>9? data.getSeconds(): '0'+ data.getSeconds();
       const time  = `${Y}-${M}-${D} ${h}:${m}:${s}`
       return time
    }
    console.log("getTime(3)",getTime(5,'after'))

相关文章

网友评论

      本文标题:js时间戳转日期

      本文链接:https://www.haomeiwen.com/subject/lpmjjktx.html