1. 程式人生 > >js 格式化時間友好的方式

js 格式化時間友好的方式

友好:剛剛、今天顯示HH:MM、昨天HH:MM、前天HH:MM、YYYY-MM-DD

最後還是手寫最笨的方式,無語,(android出身)js的這些個物件類不熟啊:

哭

/**
 * YYYY-MM-DD HH:MM:SS
 * 將一個日期格式化成友好格式,比如,1分鐘以內的返回“剛剛”,
 * 當天的返回時分,當年的返回月日,否則,返回年月日
 * @param {Object} date
 */
 formatDateToFriendly(fromDateString) {

     if(fromDateString===undefined || fromDateString===''){
         return fromDateString;
     }

     //當前日期以及對應的年月日時分秒
    var nowDate = new Date();
    let nowDateYear = nowDate.getFullYear();// 年份,注意必須用getFullYear
    let nowDateMonth = nowDate.getMonth()+1;// 月份,注意是從0-11
    let nowDateDay = nowDate.getDate();// 日期
    let nowDateHours = nowDate.getHours();//24小時制
    let nowDateMinutes = nowDate.getMinutes(); // 分鐘
    let nowDateSeconds =  nowDate.getSeconds(); // 秒

    // console.log('nowDate = '+ nowDateYear+'-'+(nowDateMonth<10?+'0'+nowDateMonth:nowDateMonth)+'-'+(nowDateDay<10?+'0'+nowDateDay:nowDateDay)
    //     +' '+(nowDateHours<10?+'0'+nowDateHours:nowDateHours)+':'+(nowDateMinutes<10?+'0'+nowDateMinutes:nowDateMinutes)+':'+
    //     (nowDateSeconds<10?+'0'+nowDateSeconds:nowDateSeconds));
    // console.log('fromDate = '+ fromDateString);

    //傳過來的日期以及對應的年月日時分秒
    var formDateArray = fromDateString.split(" ")[0].split('-');
    var fromTimeArray = fromDateString.split(" ")[1].split(':');
    let fromDateYear = formDateArray[0];// 年份,注意必須用getFullYear
    let fromDateMonth = formDateArray[1];// 月份,注意是從0-11
    let fromDateDay = formDateArray[2];// 日期
    let fromDateHours =fromTimeArray[0];//24小時制
    let fromDateMinutes = fromTimeArray[1]; // 分鐘
    let fromDateSeconds =  fromTimeArray[2]; // 秒

    //這部分是對05月01日 05時05分05秒這種進行格式化,判斷的時候用這個
    let fromDateMonthTmp = fromDateMonth;
    if(fromDateMonth.substring(0,1) === '0'){
        fromDateMonthTmp = fromDateMonth.substring(1,2)
    }
    let fromDateDayTmp = fromDateDay;
    if(fromDateDay.substring(0,1) === '0'){
        fromDateDayTmp = fromDateDay.substring(1,2)
    }
    let fromDateHoursTmp = fromDateHours;
    if(fromDateHours.substring(0,1) === '0'){
        fromDateHoursTmp = fromDateHours.substring(1,2)
    }
    let fromDateMinutesTmp = fromDateMinutes;
    if(fromDateMinutes.substring(0,1) === '0'){
        fromDateMinutesTmp = fromDateMinutes.substring(1,2)
    }
    let fromDateSecondsTmp = fromDateSeconds;
    if(fromDateSeconds.substring(0,1) === '0'){
        fromDateSecondsTmp = fromDateSeconds.substring(1,2)
    }

    // console.log('nowDateInt = '+ nowDateYear+'-'+nowDateMonth+'-'+nowDateDay+' '+nowDateHours+':'+nowDateMinutes+':'+nowDateSeconds);
    // console.log('fromDateInt = '+ fromDateYear+'-'+fromDateMonthTmp+'-'+fromDateDayTmp+' '+fromDateHoursTmp+':'+fromDateMinutesTmp+':'+fromDateSecondsTmp);

    /******************* 一分鐘之內 *******************/
    let nowDateYMDH = nowDateYear+'年'+nowDateMonth+'月'+nowDateDay+'日 '+nowDateHours;
    let fromDateYMDH = fromDateYear+'年'+fromDateMonthTmp+'月'+fromDateDayTmp+'日 '+fromDateHoursTmp;
    //需要比較的time, 1分鐘以內視作“剛剛”
    if(nowDateYMDH === fromDateYMDH && nowDateMinutes-fromDateMinutesTmp<2){
        let remainMinuteSecond = (nowDateMinutes-fromDateMinutesTmp)*60;
        let remainSecond = nowDateSeconds-fromDateSecondsTmp;
        console.log('remainMinuteSecond = '+ remainMinuteSecond+'    '+'remainSecond = '+remainSecond);
        console.log('remainSecond = '+ remainSecond);
        if (remainMinuteSecond + remainSecond < 60 * 1000) {
            return '剛剛';
        }
    }

    /*************** 今天:只顯示時間,格式為 時分      (已測試沒問題) **********/

    //需要比較的 日期yyyy年M月d
    let nowDateYMD = nowDateYear+'年'+nowDateMonth+'月'+nowDateDay+'日';
    let fromDateYMD = fromDateYear+'年'+fromDateMonthTmp+'月'+fromDateDayTmp+'日';

    //返回時分
    if (nowDateYMD === fromDateYMD){
        return fromDateHours+':'+fromDateMinutes;
    }

    /*************** 昨天:不顯示日期,顯示“昨天”,格式為:“昨天” 時分; ***********/
    /*************** 前天:不顯示日期,顯示“前天”,格式為:“前天” 時分; ***********/
    let nowDateYM = nowDateYear+'年'+nowDateMonth+'月';
    let fromDateYM = fromDateYear+'年'+fromDateMonthTmp+'月';
    if (nowDateYM === fromDateYM ){
        if(nowDateDay-fromDateDayTmp===1){
            return '昨天 '+fromDateHours+':'+fromDateMinutes;
        }else if(nowDateDay-fromDateDayTmp===2){
            return '前天 '+fromDateHours+':'+fromDateMinutes;
        }
    }

    /******** 今年其他日期:不顯示時間,僅顯示月日,格式為:X月X日; ************/
    let nowDateY = nowDateYear+'年';
    let fromDateY = fromDateYear+'年';
    if (nowDateY === fromDateY){
        return fromDateMonth+'月'+fromDateDay+'日';
    }

    /******* 其它年份:不顯示時間,僅顯示年月日,格式為:XXXX年XX月XX日; **********/
    return fromDateYear+'年'+fromDateMonth+'月'+fromDateDay+'日';
},