|
|
|
|
function formatTime(time) {
|
|
|
|
|
if (typeof time !== 'number' || time < 0) {
|
|
|
|
|
return time
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var hour = parseInt(time / 3600)
|
|
|
|
|
time = time % 3600
|
|
|
|
|
var minute = parseInt(time / 60)
|
|
|
|
|
time = time % 60
|
|
|
|
|
var second = time
|
|
|
|
|
|
|
|
|
|
return ([hour, minute, second]).map(function(n) {
|
|
|
|
|
n = n.toString()
|
|
|
|
|
return n[1] ? n : '0' + n
|
|
|
|
|
}).join(':')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function formatLocation(longitude, latitude) {
|
|
|
|
|
if (typeof longitude === 'string' && typeof latitude === 'string') {
|
|
|
|
|
longitude = parseFloat(longitude)
|
|
|
|
|
latitude = parseFloat(latitude)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
longitude = longitude.toFixed(2)
|
|
|
|
|
latitude = latitude.toFixed(2)
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
longitude: longitude.toString().split('.'),
|
|
|
|
|
latitude: latitude.toString().split('.')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
var dateUtils = {
|
|
|
|
|
UNITS: {
|
|
|
|
|
'年': 31557600000,
|
|
|
|
|
'月': 2629800000,
|
|
|
|
|
'天': 86400000,
|
|
|
|
|
'小时': 3600000,
|
|
|
|
|
'分钟': 60000,
|
|
|
|
|
'秒': 1000
|
|
|
|
|
},
|
|
|
|
|
humanize: function(milliseconds) {
|
|
|
|
|
var humanize = '';
|
|
|
|
|
for (var key in this.UNITS) {
|
|
|
|
|
if (milliseconds >= this.UNITS[key]) {
|
|
|
|
|
humanize = Math.floor(milliseconds / this.UNITS[key]) + key + '前';
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return humanize || '刚刚';
|
|
|
|
|
},
|
|
|
|
|
format: function(dateStr) {
|
|
|
|
|
var date = this.parse(dateStr)
|
|
|
|
|
var diff = Date.now() - date.getTime();
|
|
|
|
|
if (diff < this.UNITS['天']) {
|
|
|
|
|
return this.humanize(diff);
|
|
|
|
|
}
|
|
|
|
|
var _format = function(number) {
|
|
|
|
|
return (number < 10 ? ('0' + number) : number);
|
|
|
|
|
};
|
|
|
|
|
return date.getFullYear() + '/' + _format(date.getMonth() + 1) + '/' + _format(date.getDate()) + '-' +
|
|
|
|
|
_format(date.getHours()) + ':' + _format(date.getMinutes());
|
|
|
|
|
},
|
|
|
|
|
parse: function(str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串,转化为一个Date对象
|
|
|
|
|
var a = str.split(/[^0-9]/);
|
|
|
|
|
return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//获取日期的今天,明天,后天 -1是昨天,0是今天,1是后一天
|
|
|
|
|
function getDateStr(today, addDayCount) {
|
|
|
|
|
let date;
|
|
|
|
|
if (today) {
|
|
|
|
|
date = new Date(today);
|
|
|
|
|
} else {
|
|
|
|
|
date = new Date();
|
|
|
|
|
}
|
|
|
|
|
date.setDate(date.getDate() + addDayCount); //获取AddDayCount天后的日期
|
|
|
|
|
let y = date.getFullYear();
|
|
|
|
|
let m = date.getMonth() + 1; //获取当前月份的日期
|
|
|
|
|
let d = date.getDate();
|
|
|
|
|
if (m < 10) {
|
|
|
|
|
m = '0' + m;
|
|
|
|
|
};
|
|
|
|
|
if (d < 10) {
|
|
|
|
|
d = '0' + d;
|
|
|
|
|
};
|
|
|
|
|
console.log(y + "-" + m + "-" + d)
|
|
|
|
|
return y + "-" + m + "-" + d;
|
|
|
|
|
}
|
|
|
|
|
//非空校验
|
|
|
|
|
const checkNotEmpty = (str) => {
|
|
|
|
|
return typeof(str) !== 'undefined' && str !== null && str !== '' ? true : false
|
|
|
|
|
}
|
|
|
|
|
// 获取日期
|
|
|
|
|
function getDay(d, num) {
|
|
|
|
|
const date = new Date(new Date(d).setDate(new Date(d).getDate() + num))
|
|
|
|
|
const year = date.getFullYear()
|
|
|
|
|
const month = (date.getMonth() + 1 + "").padStart(2, "0")
|
|
|
|
|
const day = (date.getDate() + "").padStart(2, "0")
|
|
|
|
|
return `${year}-${month}-${day}`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 计算环比
|
|
|
|
|
function calcMom(cur, data) {
|
|
|
|
|
console.log(cur,"环比==========",data);
|
|
|
|
|
return !checkNotEmpty(cur) || !checkNotEmpty(data) ? '' : data === 0 && parseInt(cur) === 0 ? 0 : data === 0 && parseInt(cur) > 0 ? 100 : (parseInt(cur) - data) / data * 100;
|
|
|
|
|
}
|
|
|
|
|
// 计算同比
|
|
|
|
|
function calcYoy(cur, data) {
|
|
|
|
|
console.log(cur,"同比==========",data);
|
|
|
|
|
return !checkNotEmpty(cur) || !checkNotEmpty(data) ? '' : data === 0 && parseInt(cur) === 0 ? 0 : data === 0 && parseInt(cur) > 0 ? 100 : (parseInt(cur) - data) / data * 100;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 是否是闰年
|
|
|
|
|
function isLeap(year){
|
|
|
|
|
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 返回去年日期
|
|
|
|
|
function lastYearDate(d) {
|
|
|
|
|
const date = new Date(d)
|
|
|
|
|
const year = date.getFullYear()
|
|
|
|
|
const month = date.getMonth() + 1
|
|
|
|
|
const day = date.getDate()
|
|
|
|
|
const lastYear = year - 1
|
|
|
|
|
const thisYearisLeap = isLeap(year)
|
|
|
|
|
const lastYearIsLeap = isLeap(lastYear)
|
|
|
|
|
// 特殊情况:去年是否是闰年 或 今年是闰年且为12月31日
|
|
|
|
|
if (lastYearIsLeap || (thisYearisLeap && month == 12 && day == 31)) return getDay(d, -366)
|
|
|
|
|
return getDay(d, -365)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export {
|
|
|
|
|
formatTime,
|
|
|
|
|
formatLocation,
|
|
|
|
|
getDateStr,
|
|
|
|
|
dateUtils,
|
|
|
|
|
checkNotEmpty,
|
|
|
|
|
lastYearDate,
|
|
|
|
|
calcMom,
|
|
|
|
|
calcYoy
|
|
|
|
|
}
|