js 顯示友好的時間格式【剛剛、幾秒前,幾小時,幾天前(3天內) 時間格式化】

來源:互聯網
上載者:User

標籤:div   logs   log   cti   else   float   ram   return   style   

/** * 毫秒轉換友好的顯示格式 * 輸出格式:21小時28分鐘15秒 * @param  {[type]} time [description] * @return {[type]}      [description] */function timeToDate(time) {    // 擷取目前時間戳    var currentTime = parseInt(new Date().getTime()/1000);    var diffTime     = currentTime-time;    var second         = 0;    var minute         = 0;    var hour         = 0;    if (null != diffTime && "" != diffTime) {        if (diffTime > 60 && diffTime < 60 * 60) {            diffTime = parseInt(diffTime / 60.0) + "分鐘" + parseInt((parseFloat(diffTime / 60.0) - parseInt(diffTime / 60.0)) * 60) + "秒";        }        else if (diffTime >= 60 * 60 && diffTime < 60 * 60 * 24) {            diffTime = parseInt(diffTime / 3600.0) + "小時" + parseInt((parseFloat(diffTime / 3600.0) -                parseInt(diffTime / 3600.0)) * 60) + "分鐘" +                parseInt((parseFloat((parseFloat(diffTime / 3600.0) - parseInt(diffTime / 3600.0)) * 60) -                parseInt((parseFloat(diffTime / 3600.0) - parseInt(diffTime / 3600.0)) * 60)) * 60) + "秒";        }        else {            //超過1天            var date = new Date(parseInt(time) * 1000);            diffTime = date.getFullYear()+"/"+(date.getMonth()+1)+"/"+date.getDate();            //diffTime = parseInt(diffTime) + "秒";        }    }    return diffTime;}
/** * 毫秒轉換友好的顯示格式 * 輸出格式:21小時前 * @param  {[type]} time [description] * @return {[type]}      [description] */function dateStr(date){    //擷取js 時間戳記    var time=new Date().getTime();    //去掉 js 時間戳記後三位,與php 時間戳記保持一致    time=parseInt((time-date*1000)/1000);    //儲存轉換值     var s;    if(time<60*10){//十分鐘內        return ‘剛剛‘;    }else if((time<60*60)&&(time>=60*10)){        //超過十分鐘少於1小時        s = Math.floor(time/60);        return  s+"分鐘前";    }else if((time<60*60*24)&&(time>=60*60)){         //超過1小時少於24小時        s = Math.floor(time/60/60);        return  s+"小時前";    }else if((time<60*60*24*3)&&(time>=60*60*24)){         //超過1天少於3天內        s = Math.floor(time/60/60/24);        return s+"天前";    }else{         //超過3天        var date= new Date(parseInt(date) * 1000);        return date.getFullYear()+"/"+(date.getMonth()+1)+"/"+date.getDate();    }}

 

js 顯示友好的時間格式【剛剛、幾秒前,幾小時,幾天前(3天內) 時間格式化】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.