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+'日';
},
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

漠天515

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值