vue utils.js的方法及使用

1. 处理时间戳

dateFormatter: function(time, format = '{y}-{m}-{d} {h}:{i}:{s}') {
		let date
		if (time instanceof Date) {
			date = time
		} else if (typeof time === 'number') {
			const timestamp = Number((time.toString() + '000').substr(0, 13))
			date = new Date(timestamp)
		} else if (typeof time === 'string') {
			date = new Date(time)
		} else {
			return null
		}

		if (date.toString() === 'Invalid Date') return null
		const formatObj = {
			y: date.getFullYear(),
			m: date.getMonth() + 1,
			d: date.getDate(),
			h: date.getHours(),
			i: date.getMinutes(),
			s: date.getSeconds()
		}
		const timeStr = format.replace(/{(y|m|d|h|i|s)+}/g, (result, key) => {
			let value = formatObj[key]
			if (result.length > 0 && value < 10) {
				value = '0' + value
			}
			return value || 0
		})
		return timeStr
	}

2. 处理存储和流量类型数据

flowFormatObj: function(bytes, unit, factor) {
		const obj = {}
		if (!bytes || bytes === 0) return {
			size: 0,
			unit: 'Byte'
		}
		const k = factor || 1024
		const sizes = ['Byte', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
		if (unit) {
			const i = sizes.indexOf(unit)
			if (i !== -1) {
				obj.size = (bytes / Math.pow(k, i)).toFixed(i === 0 ? 0 : 2)
				obj.unit = sizes[i]
			} else {
				obj.size = bytes
				obj.unit = unit
			}
			return obj
		} else {
			const i = Math.ceil(Math.log(bytes) / Math.log(k))
			if ((bytes / Math.pow(k, i)) < 1) { // 不到一个单位,降一个单位
				obj.size = (bytes / Math.pow(k, i - 1)).toFixed((i - 1) === 0 ? 0 : 2)
				obj.unit = sizes[i - 1]
				return obj
			}
			obj.size = (bytes / Math.pow(k, i)).toFixed(i === 0 ? 0 : 2)
			obj.unit = sizes[i]
			return obj
		}
	},
	fileSizeFormat: function(quantity, zeroUnit, factor) {
		if (!quantity || quantity === 0 || quantity < 0) {
			if (zeroUnit) {
				return '0 ' + zeroUnit
			} else {
				return '0 Byte'
			}
		}
		const k = factor || 1024
		const sizes = ['Byte', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
		const i = Math.ceil(Math.log(quantity) / Math.log(k))
		if ((quantity / Math.pow(k, i)) < 1) {
			return (quantity / Math.pow(k, i - 1)).toFixed((i - 1) === 0 ? 0 : 2) + ' ' + sizes[i - 1]
		} else {
			return (quantity / Math.pow(k, i)).toFixed(i === 0 ? 0 : 2) + ' ' + sizes[i]
		}
	}

3. 处理金钱数据-3位一痘

toThousands(num) {
	  num = (num || 0).toString()
	  var result = ''
	  if (num.split(".")[1]) {
	    var numInt = num.split('.')[0],
	      numFlo = num.substring(num.indexOf('.'))
	    result = this.toThousandsOfInt(numInt) + numFlo;
	  } else {
	    result = this.toThousandsOfInt(num)
	  }
	  return result
	},
	toThousandsOfInt(num) {
	  num = (num || 0).toString()
	  var result = ''
	  if (num.indexOf(',') < 0) {
	    while (num.length > 3) {
	      result = ',' + num.slice(-3) + result
	      num = num.slice(0, num.length - 3)
	    }
	    if (num) {
	      result = num + result
	    }
	  } else {
	    result = num
	  }
	
	  return result;
	}

4. 计算时间差

toComputationTime(ts){
	    if (ts) {
	      var  DAY =  24 * 60 * 60
	      var HOUR =  60 * 60;
	      var MINUTE = 60;
	      var day = Math.floor(ts / DAY);
	      var hour = Math.floor(ts % DAY / HOUR);
	      if(day>=1){
	          hour = day * 24 + hour;
	      }
	      var min = Math.floor(ts % DAY % HOUR / MINUTE);
	      var sec = ts % DAY % HOUR % MINUTE;
	      var h = hour + "";
	      var m = min + "";
	      var s = sec + "";
	      if(hour<10){
	          h = "0" + h;
	      }
	      if(min<10){
	          m = "0" + m;
	      }
	      if(sec<10){
	          s = "0" + s;
	      }
	      return h + ":" + m + ":" + s;
	  } else {
	    return"00:00:00"
	  }
	},
	toComputationHhTime(ts){
	    if (ts) {
	      var  DAY =  24 * 60 * 60
	      var HOUR =  60 * 60;
	      var MINUTE = 60;
	      var day = Math.floor(ts / DAY);
	      var hour = Math.floor(ts % DAY / HOUR);
	      if(day>=1){
	          hour = day * 24 + hour;
	      }
	      var min = Math.floor(ts % DAY % HOUR / MINUTE);
	      var h = hour + "";
	      var m = min + "";
	      if(hour<10){
	          h = "0" + h;
	      }
	      if(min<10){
	          m = "0" + m;
	      }
	      return h + "小时" + m + "分钟" ;
	  } else {
	    return""
	  }
	}

5. 保留几位小数

numberFormat:function(number, decimals, dec_point, thousands_sep) {
	  if(decimals == undefined) decimals = 2;
	  if(dec_point == undefined) dec_point = '.';
	  if(thousands_sep == undefined) thousands_sep = ',';
	  var r = number.toFixed(decimals);
	  var nStr = r + '';
	  var x = nStr.split(dec_point);
	  var x1 = x[0];
	  var x2 = x.length > 1 ? dec_point + x[1] : '';
	  var rgx = /(\d+)(\d{3})/;
	  while (rgx.test(x1)) {
	      x1 = x1.replace(rgx, '$1' + ',' + '$2');
	  }
	  return x1 + x2;
	}

使用
mai.js

import Utils from '@util/utils'
Vue.prototype.Utils = Utils
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值