美文网首页
H5前端 - 总结JavaScript, Date()时间格式和

H5前端 - 总结JavaScript, Date()时间格式和

作者: sponing | 来源:发表于2018-05-22 14:27 被阅读0次
    
    /** 转换日期格式
     * @param date : 日期格式|String类型 (如:'2012-12-12' | '2012年12月12日' | new Date())
     * @param format : String类型 (如: 'yyyy年MM月dd日'或'yyyy年MM月dd日 hh时mm分ss秒',默认'yyyy-MM-dd')
     * @example C.Utils.parseDateFormat(new Date(), 'yyyy年MM月dd日') 输出:'2014年04月29日'
     * @example C.Utils.parseDateFormat(new Date()) 输出:'2014-04-29'
     * @exmaple C.Utils.parseDateFormat('2014-05-07 16:09:47','yyyy年MM月dd日 hh时mm分ss秒')
     *          输出:'2014年05月07日 16时09分47秒'
     **/
    const parseDateFormat = (date, format)=> {
        let year = '',
            month = '',
            day = '',
            hours = '',
            minutes = '',
            seconds = '',
            dateMatch;
        if (!date) {
            date = new Date();
        }
        function replaceDate(val) {
            return (val + '').toString().replace(/^(\d{4})(\d{2})(\d{2})$/, '$1/$2/$3');
        }
    
        if (date.length === 8) {
            date = replaceDate(date);
        } else if (date.length === 18) {
            date = replaceDate(date.substr(6, 8));
        } else if (date.length === 15) {
            // 19几几年的才有15位身份证的可能. 15位默认添加19
            date = replaceDate('19' + date.substr(6, 6));
        }
        format = format || 'yyyy-MM-dd';
        if (typeof date === 'string') {
            dateMatch = date.match(this.RegexMap.parseDateFormat);
            if (dateMatch) {
                year = dateMatch[1];
                month = dateMatch[2];
                day = dateMatch[3];
                hours = dateMatch[5];
                minutes = dateMatch[6];
                seconds = dateMatch[7];
            }
        } else {
            year = date.getFullYear();
            month = date.getMonth() + 1;
            day = date.getDate();
            hours = date.getHours();
            minutes = date.getMinutes();
            seconds = date.getSeconds();
        }
        month = addZero(month);
        day = addZero(day);
        hours = addZero(hours);
        minutes = addZero(minutes);
        seconds = addZero(seconds);
        return format.replace('yyyy', year).replace('MM', month).replace('dd', day).replace('hh', hours).replace('mm', minutes).replace('ss', seconds);
    };
    
    /**
     * 计算时间差
     * @param startDate 开始时间的时间戳
     * @param endDate 结束时间的时间戳
     * @returns {*} 输出:时间差
     */
    const computeDate = (startDate, endDate)=> {
        let y, m, d, start, end;
        start = startDate ? new Date(startDate) : new Date(0);
        end = endDate ? new Date(endDate) : new Date(0);
        y = end.getFullYear() - start.getFullYear() + 1;
        m = end.getMonth() - start.getMonth();
        d = end.getDate() - start.getDate();
        if (y >= 0) {
            (m < 0 || (m === 0) && (d < 0 || d === 0)) && y--;
        } else {
            y = 0;
        }
        return y;
    };
    
    /*
    * 相互转化中国标准时间
    * @param 2016-11-11T10:52:28+08:00 <=> 2016-11-11 10:52:28
    * 获取当前的年月日 时分秒
    * @return 2018-03-19 16:57:44
     */
    const formatTime = date=> {
      const year = date.getFullYear()
      const month = date.getMonth() + 1
      const day = date.getDate()
      const hour = date.getHours()
      const minute = date.getMinutes()
      const second = date.getSeconds()
    
      return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute].map(formatNumber).join(':')
      // return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
    };
    
    const getDate = (type) => {
      var date = new Date();
      var year = date.getFullYear();
      var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth();
      var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate()
      var hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
      var minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
      var second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
    
      if (type == "approvelsn") {
        var randomNum = (Math.random() * 9 + 1) * 100000 + "";
        var newNum = randomNum.split(".")
        return year + month + day + hour + minute + second + newNum[0];
      } else if (type == "time") {
        return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
      }
    }
    

    相关文章

      网友评论

          本文标题:H5前端 - 总结JavaScript, Date()时间格式和

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