123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- var numToArray = function(num) {
- var arr = [];
- for (var i = 0; i < num; i++) {
- arr[i] = i;
- }
- return arr;
- }
- var formatNumber = function(n) {
- n = n.toString()
- return n[1] ? n : '0' + n
- }
- var strToInt = function(n) {
- return parseInt(n)
- }
- var minuteToHour = function(minute) {
- // minute = Math.ceil(minute/60)
- var hour = Math.floor(minute / 60)
- var remainMinute = minute - hour * 60
- var minutehour = ""
- if (hour != 0) {
- minutehour = remainMinute != 0 ? (hour + "小时" + remainMinute + "分钟") : (hour + "小时")
- } else {
- minutehour = remainMinute != 0 ? (remainMinute + "分钟") : (hour + "小时")
- }
- return minutehour
- //return remainMinute !=0 ? (hour+"小时"+remainMinute+"分钟"):(hour+"小时")
- }
- /**
- * 时间戳格式化时间
- */
- var formatTime = function(datetime) {
- if (datetime == 0 ||!datetime) {
- return ""
- }
- var date = getDate(datetime * 1000);
- var year = date.getFullYear()
- var month = date.getMonth() + 1
- var day = date.getDate()
- var hour = date.getHours()
- var minute = date.getMinutes()
- var second = date.getSeconds()
- return year + '-' + formatNumber(month) + '-' + formatNumber(day) + ' ' + formatNumber(hour) + ':' +
- formatNumber(minute) + ':' + formatNumber(second);
- }
- // 时间戳格式化 返回日期
- var formatGetDate = function(datetime) {
- var date = getDate(datetime * 1000);
- var year = date.getFullYear()
- var month = date.getMonth() + 1
- var day = date.getDate()
- return year + '-' + formatNumber(month) + '-' + formatNumber(day);
- }
- // 时间戳格式化 返回x月x日 x:x
- var formatTimeDate = function(datetime) {
- if (datetime == 0) {
- return ""
- }
- var date = getDate(datetime * 1000);
- var month = date.getMonth() + 1
- var day = date.getDate()
- var hour = date.getHours()
- var minute = date.getMinutes()
- var second = date.getSeconds()
- return formatNumber(month) + '月' + formatNumber(day) + '日 ' + formatNumber(hour) + ':' + formatNumber(minute);
- }
- // 时间戳格式化 返回x-x x:x
- var formatTimeSecond = function(datetime) {
- if (datetime == 0) {
- return ""
- }
- var date = getDate(datetime * 1000);
- var month = date.getMonth() + 1
- var day = date.getDate()
- var hour = date.getHours()
- var minute = date.getMinutes()
- var second = date.getSeconds()
- return formatNumber(month) + '-' + formatNumber(day) + ' ' + formatNumber(hour) + ':' + formatNumber(minute);
- }
- var formatDateSplit = function(dateString) {
- const parts = dateString.split('-');
- return parts[0] + parts[1] + parts[2];
- }
- var daysBetweenTimestamps = function(timestamp1, timestamp2) {
- const date1 = formatDateSplit(formatGetDate(timestamp1));
- const date2 = formatDateSplit(formatGetDate(timestamp2));
- const diff = date2 - date1;
- return diff;
- }
- /**
- * 隐藏字符串 多余字符使用*替代
- */
- var maskStr = function(str, replace, len) {
- if (typeof str != 'string')
- return '';
- if (str.length < len) return str;
- return str.substring(0, len) + replace + replace + replace;
- }
- /**
- * 将字符串分割成数组
- */
- var splitx = function(str, splitstr) {
- if (typeof str != 'string')
- return [];
- return str.split(splitstr);
- }
- var toFix = function(value) {
- //return value.toFixed(2)
- return parseFloat(parseFloat(value).toFixed(2)) //此处2为保留两位小数
- }
- var toFixLength = function(value) {
- return parseFloat(parseFloat(value).toFixed(2)).toString().length //此处2为保留两位小数
- }
- var toFixThree = function(value) {
- return parseFloat(parseFloat(value).toFixed(3)) //此处3为保留两位小数
- }
- var toFixCeli = function(value) {
- return parseFloat(parseFloat(Math.ceil(value * 100) / 100).toFixed(2))
- //return parseFloat(parseFloat(value).toFixed(2))//此处2为保留两位小数
- }
- var formatToday = function() {
- var date = getDate()
- var year = date.getFullYear()
- var month = date.getMonth() + 1
- var day = date.getDate()
- var wday = date.getDay()
- var weekday = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
- return [formatNumber(month) + '月' + formatNumber(day) + '日', weekday[wday]];
- }
- //格式化时间 12:00
- var timestampToHHMM = function(timestamp) {
- var date = getDate(timestamp);
- var hours = date.getHours().toString().padStart(2, '0');
- var minutes = date.getMinutes().toString().padStart(2, '0');
- return hours + ':' + minutes;
- }
- var formatDistance = function(distanceMeters) {
- // 判断距离是否超过1000米
- // let distanceMeters=Number(_distanceMeters)
- if (distanceMeters >= 1000) {
- // 如果超过1000米,则转换为千米并返回
- return (distanceMeters / 1000).toFixed(2) + 'km';
- } else {
- // 否则直接返回米
- return distanceMeters.toFixed(0) + 'm';
- }
- }
- var formatWeight = function(weightMeters) {
- if (weightMeters >= 1000) {
- return (weightMeters / 1000).toFixed(2) + 'kg';
- } else {
- return weightMeters.toFixed(0) + 'g';
- }
- }
- var countToDay = function(count, unit) {
- var data = '';
- switch (unit - 0) {
- case 1: //日
- data = count
- break;
- case 2: //月
- data = count * 30
- break;
- case 4: //时
- data = count
- break;
- case 6: //周
- data = count * 7
- break;
- case 7: //季
- data = count * 90
- break;
- }
- return data;
- }
- export default {
- numToArray: numToArray,
- formatTime: formatTime,
- formatGetDate: formatGetDate,
- formatTimeDate: formatTimeDate,
- formatTimeSecond: formatTimeSecond,
- daysBetweenTimestamps: daysBetweenTimestamps,
- formatDateSplit: formatDateSplit,
- maskStr: maskStr,
- splitx: splitx,
- toFix: toFix,
- toFixLength: toFixLength,
- toFixCeli: toFixCeli,
- formatToday: formatToday,
- minuteToHour: minuteToHour,
- strToInt: strToInt,
- timestampToHHMM: timestampToHHMM,
- formatDistance: formatDistance,
- formatWeight: formatWeight,
- toFixThree: toFixThree,
- countToDay: countToDay
- }
|