瓜笑

导航

前端常用的utils:时间单位格式化,判断是否安卓,IOS等等

大数据项目中用到一些单位转化方法如下:

    isAndroid() {
        return /android/i.test(navigator.userAgent);
    }
    isIOS() {
        return /iPhone|iPad|iPod/i.test(navigator.userAgent);
    }
 /**
     * 获取当前日期是当年的第几周
     * @param {String} date yyyy-MM-dd
     */
    getYearWeek(date) {
        const [year, month, day] = date.split('-')
        const date1 = new Date(year, parseInt(month) - 1, day)
        const date2 = new Date(year, 0, 1)
        const d = Math.round((date1.valueOf() - date2.valueOf()) / 86400000)
        return Math.ceil((d + ((date2.getDay() + 1) - 1)) / 7)
    }
  /**
     * 计算年份第几周的周一和周日的日期
     * @param {String} year 年份
     * @param {Number} n 第几周
     */
    calcWeekMondayAndSunday(year, n) {
        const date = year + '-01-01'
        const format = 'YYYY-MM-DD'
        const dateFormat = 'MM-DD'
        const currentDate = moment(date, format).add(7 * (n - 1), 'days').format(format)
        const weekOfday = moment(currentDate, format).format('E')
        const Monday = moment(currentDate, format).subtract(weekOfday - 1, 'days').format(dateFormat)
        const Sunday = moment(currentDate, format).add(7 - weekOfday, 'days').format(dateFormat)
        // const Saturday = this.dateChange(-2, moment(currentDate, format).subtract(weekOfday - 1, 'days').format(format))
        // const Friday = this.dateChange(-2, moment(currentDate, format).add(7 - weekOfday, 'days').format(format))
        const Saturday = moment(currentDate, format).subtract(4, 'days').format(dateFormat)
        const Friday = moment(currentDate, format).subtract(-2, 'days').format(dateFormat)
        return {
            Monday,
            Sunday,
            Saturday,
            Friday
        }
    },
    /**
     * 格式化数字单位,返回数字和中文单位
     * @param {Number} number 数字
     * @return {Object} { value, unit }
     */
    formatNumberUnit(number) {
        number = Number(number)
        if (isNaN(number)) {
            return {
                value: number || '-',
                unit: ''
            }
        }
        const param = {}
        const k = 10000
        const sizes = ['', '万', '亿', '万亿']
        let i
        if (number < k) {
            param.value = number
            param.unit = ''
        } else {
            i = Math.floor(Math.log(number) / Math.log(k))
            param.value = (number / Math.pow(k, i)).toFixed(2)
            param.unit = sizes[i]
        }
        return param
    },
/**
     * 根据后台返回的单位和数字,格式化单位为人,还原为原始数据
     * @param {Aarry} object {unit, value, mom, yoy}对象数据
     */
      formatProvinceData(dataList) {
            const unitValue = {
                '亿': 100000000,
                '万': 10000,
                '': 1
            }
            return dataList.map(item => {
                const unit = item.unit.replace('人', '')
                const multiplier = unitValue[unit]
                item.unit = '人'
                item.value = Math.ceil(item.value * multiplier)
                return item
            })
        }

posted on 2020-11-20 14:16  瓜笑  阅读(146)  评论(0编辑  收藏  举报