发表时间分别显示

timess 时间戳
//计算时间差
function timesshow(timess){
    var time1=new Date();  //现在时间
    var time2=new Date(timess);  //结束时间
    var time3=time1.getTime()-time2.getTime()  //时间差的毫秒数
    //计算出相差星期    var weeks=Math.floor(time3/(7*24*3600*1000));
    //计算出相差天数
    var days=Math.floor(time3/(24*3600*1000));
    //计算出小时数
    var leave1=time3%(24*3600*1000)    //计算天数后剩余的毫秒数
    var hours=Math.floor(leave1/(3600*1000));
    //计算相差分钟数
    var leave2=leave1%(3600*1000)        //计算小时数后剩余的毫秒数
    var minutes=Math.floor(leave2/(60*1000));
    //计算相差秒数
    var leave3=leave2%(60*1000)      //计算分钟数后剩余的毫秒数
    var seconds=Math.round(leave3/1000);
    if(weeks!=0){
        return formatDate(timess);//自己解析时间戳
    }else if(weeks==0&&days!=0){
        return days+"天前发表";
    }else if(weeks==0&&days==0&&hours!=0){
        return  hours+"小时前发表";
    }else if(weeks==0&&days==0&&hours==0&&minutes!=0){
        return  minutes+" 分钟前发表";
    }else if(weeks==0&&days==0&&hours==0&&minutes==0&&seconds!=0){
        return "刚发表:"+seconds+" ";
    }
}

//处理时间戳
function formatDate(timessnow) {
    var now=new Date(timessnow);
    var year = now.getFullYear();
    var month = now.getMonth() < 9 ? "0" + (now.getMonth() + 1) : now.getMonth() + 1;
    var date = now.getDate() < 10 ? "0" + now.getDate() : now.getDate();
    var hour = now.getHours() < 10 ? "0" + now.getHours() : now.getHours();
    var minute = now.getMinutes() < 10 ? "0" + now.getMinutes() : now.getMinutes();
    var second = now.getSeconds() < 10 ? "0" + now.getSeconds() : now.getSeconds();
    return year + "" + month + "" + date + " " +
    hour + ":" + minute + ":" + second;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值