html5开发 消息时间显示处理

项目开发过程中,聊天模块需要对信息时间进行处理(聊天用的环信sdk),自己写了一个插件如下:
	定义一个函数showMessageTime(timestamp,timeShowPlance), 有两个参数,第一个参数接收时间戳,第一个参数接收显示的地点,如果是列表界面,则对应周几时不显示后面具体的时间;如果是聊天页面,则除了显示周几 还显示具体的消息时间(时、分)

function showMessageTime(timestamp,timeShowPlace){
    /** 消息发送时间 **/
    timestamp = parseInt(timestamp)
    var msgTime = new Date(timestamp);
    var msgTimeYear = msgTime.getUTCFullYear()
    var msgTimeMonth = (msgTime.getUTCMonth()+1)
    var msgTimeDate = msgTime.getUTCDate()
    var hms = "";
    hms += " " + msgTime.getHours() + ":"
    hms += " " + msgTime.getMinutes() >= 10 ? msgTime.getMinutes():'0'+msgTime.getMinutes()
    /** 获取本地时间 **/
    var myDate = new Date();
    var myDateYear = myDate.getUTCFullYear()
    var myDateMonth = myDate.getUTCMonth()+1
    var myDateDate = myDate.getUTCDate()
    var dateMinus = msgTimeDate - myDateDate

    /** 消息发送时间和本地时间为同一天 **/
    if (msgTimeYear == myDateYear && msgTimeMonth == myDateMonth && msgTimeDate == myDateDate) {
        return hms;
    }
    /** 消息发送时间在一周范围内 **/
    else if (msgTimeYear == myDateYear && msgTimeMonth == myDateMonth && dateMinus >= -6 && dateMinus <= -1){
        for (var i = 0 ; i <= 6 ; i++){
            if( myDate.getDay() == i) {
                console.log('i='+i)
                if (dateMinus == -6) {
                    return '周'
                        + (i==0?'一':(i==1?'二':(i==2?'三':(i==3?'四':(i==4?'五':(i==5?'六':(i==6?'日':' ')))))))
                        + ' ' + (timeShowPlace=="messageList"?'':hms)
                } else if (dateMinus == -5) {
                    return '周'
                        +(i==0?'二':(i==1?'三':(i==2?'四':(i==3?'五':(i==4?'六':(i==5?'日':(i==6?'日':' ')))))))
                        + ' ' + (timeShowPlace=="messageList"?'':hms)
                } else if (dateMinus == -4) {
                    return '周'
                        +(i==0?'三':(i==1?'四':(i==2?'五':(i==3?'六':(i==4?'日':(i==5?'一':(i==6?'二':' ')))))))
                        + ' ' + (timeShowPlace=="messageList"?'':hms)
                } else if (dateMinus == -3) {
                    return '周'
                        +(i==0?'四':(i==1?'五':(i==2?'六':(i==3?'日':(i==4?'一':(i==5?'二':(i==6?'三':' ')))))))
                        + ' ' + (timeShowPlace=="messageList"?'':hms)
                } else if (dateMinus == -2) {
                    return '周'
                        +(i==0?'五':(i==1?'六':(i==2?'日':(i==3?'一':(i==4?'二':(i==5?'三':(i==6?'四':' ')))))))
                        + ' ' + (timeShowPlace=="messageList"?'':hms)
                } else {
                    return '昨天'
                }
            }
        }
    }
    /** 消息发送时间在一周范围外 **/
    else {
        //console.log('一周范围外:'+dateMinus)
        return msgTimeYear+'/'+msgTimeMonth+'/'+msgTimeDate
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章