美文网首页
React Native js常用时间日期处理方法

React Native js常用时间日期处理方法

作者: fangcaiwen | 来源:发表于2018-08-15 09:28 被阅读0次

    1.把2018-08-15 10:48:46转成date类

     /**
       * 可把- replace成/
       * @param dateString
       * @return Date
       */
     static parserDateString(dateString) {
        if (dateString) {
          const regEx = new RegExp('\\-', 'gi');
          const validDateStr = dateString.replace(regEx, '/');
          const milliseconds = Date.parse(validDateStr);
          return new Date(milliseconds);
        }
      }
    

    2.根据当前的时间加上几年或者几天,常用于几年后或者几天后等处理

    static addDataTime(thisTime,num,kind) {
        const newTime = new Date();
        if(kind =='year') {
          newTime.setFullYear(thisTime.getFullYear()+num);
        }else{
          newTime.setDate(thisTime.getDate()+num);
        }
        return newTime;
      }
    
    

    3.比较时间大小,常用于开始时间不能大于结束时间等处理

    static compareTime(startTime,endTime) {
        const startTimes = startTime.getTime();
        const endTimes = endTime.getTime();
        if(endTimes <= startTimes) {
          return false;
        }else{
          return true;
        }
      }
    
    1. 时间转换成刚刚,几分钟前,几小时之前,几天前等,常用于展示记录消息评论时间等
     static dateToMsgTime(dataString) {
        const dateTime = DateUtil.parserDateString(dataString);
        const now = new Date();
        const nowTimeSpan = now.getTime();
        const oldTimeSpan = dateTime.getTime();
        let result = '';
        const milliseconds = nowTimeSpan-oldTimeSpan;
        if (milliseconds <= 1000 * 60 * 1) {
          result = '刚刚';
        }else if(1000 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60) {
          result = `${Math.round((milliseconds / (1000 * 60)))}分钟前`;
        }else if(1000 * 60 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24) {
          result = `${Math.round(milliseconds / (1000 * 60 * 60))}小时前`;
        }else if(1000 * 60 * 60 * 24 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24 * 2) {
          result = `一天前${dataString.substr(11,5)}`;
        }else if(1000 * 60 * 60 * 24*2 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24 * 3) {
          result = `两天前${dataString.substr(11,5)}`;
        }else{
          result = dataString.substr(0,10);
        }
        return result;
      }
    

    5.日期转化成今天,昨天,日期等。输入格式为“2020-05-27”

     // 处理日期
        dealData = (data) => {
            if (data.length == 10) {
                let itemDate = Date.parse(new Date(data).toLocaleDateString()) / 1000;
                let curr = Date.parse(new Date()) / 1000;
                let rel = Math.floor(parseInt(curr - itemDate) / 60 / 60 / 24);
                if (rel == 0) {
                    return "今天";
                } else if (rel == 1) {
                    return "昨天"
                }
            }
            return data.substr(2).replace(/-/g, "/");
        };
    

    6.计算过去了多少天

    function howData(mdata){
       let d = new Date(mdata);
       let reduce = new Date().getTime()-d.getTime()
       return reduce/1000/3600/24 
    }
    
    howData("2019-07-24")
    // 412.0910024189814
    

    相关文章

      网友评论

          本文标题:React Native js常用时间日期处理方法

          本文链接:https://www.haomeiwen.com/subject/oaxsvftx.html