阅读:1267回复:2

日期格式发布问题。

楼主#
更多 发布于:2020-10-12 16:52
怎么将 2020-10-12 16:47 改成几分钟前 几小时前 几天前 几个月前,这种格式?
1楼#
发布于:2020-10-12 17:24
可以使用js来输出时间。

/**
 * 毫秒转换友好的显示格式
 * 输出格式: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();
    }
}
 
2楼#
发布于:2020-10-12 18:17
海洋:可以使用js来输出时间。回到原帖
调用方法呢?
游客

返回顶部