zoukankan      html  css  js  c++  java
  • js 显示友好的时间格式【刚刚、几秒前,几小时,几天前(3天内) 时间格式化】

    /**
     * 毫秒转换友好的显示格式
     * 输出格式: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();
        }
    }
  • 相关阅读:
    #Leetcode# 219. Contains Duplicate II
    #Leetcode# 203. Remove Linked List Elements
    #Leetcode# 141. Linked List Cycle
    #Leetcode# 128. Longest Consecutive Sequence
    linux——shell解释
    Linux——互联网搜索引擎nbtscan是一个扫描WINDOWS网络NetBIOS信息的小工具
    Linux——网络端口的状态netstat、ifconfig
    连不上网的原因
    jquery基础笔记
    网址收藏
  • 原文地址:https://www.cnblogs.com/shcolo/p/6773975.html
Copyright © 2011-2022 走看看