1. 生成数字范围内的随机数
/**
* 生成数字范围内的随机数
* @param min 最小数字
* @param max 最大数字
* @returns number类型
*/
function random(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
2. 转换时间字符串为大致时间描述
/**
* 转换时间字符串为大致时间描述
* @param date 日期:2022-03-29 09:08:11
* @returns 时间字符串:1个月前
*/
function makeChTime(date) {
var inDate = new Date()
var curDate = new Date()
var yearAndTime = date.split(' ')
var fullYear = yearAndTime[0].split('-')
var time = yearAndTime[1].split(':')
time[2] = time[2].split('.')[0]
inDate.setFullYear(Number(fullYear[0]), Number(fullYear[1]) - 1, Number(fullYear[2]))
inDate.setHours(Number(time[0]), Number(time[1]), Number(time[2]))
var str = ''
inDate.setMilliseconds(0)
curDate.setMilliseconds(0)
var YEAR1 = 1000 * 60 * 60 * 24 * 365
var YEAR2 = 1000 * 60 * 60 * 24 * 365 * 2
var DAY = 1000 * 60 * 60 * 24
var HOUR = 1000 * 60 * 60
var MIN = 1000 * 60
var diff = Number(curDate) - Number(inDate)
if (diff - YEAR2 >= 0) {
//判断是否是两年以上
str += inDate.getFullYear() + '年'
str += inDate.getMonth() + 1 + '月' + inDate.getDate() + '日'
}
else {
if (diff - YEAR1 >= 0) {
//判断是否是1年以上
str += '1年前'
}
else {
var subdaynum = -1
var workmonthnum = 0
for (var i = inDate.getTime()
var days = new Date(i)
subdaynum++
if (days.getDate() == 1) {
workmonthnum++
}
}
if (subdaynum >= 31) {
//相差天数是否大于31天
str += workmonthnum + '个月前'
}
else {
if (subdaynum >= 1) {
//相差天数是否大于1天
str += subdaynum + '天前'
}
else {
var h = parseInt(diff / HOUR + '')
if (h >= 1) {
//相差时间是否大于1小时
if (curDate.getDate() - inDate.getDate() == 1 && h > 12) {
//是否跨日期相差超过12小时
str += '1天前'
}
else {
str += h + '小时前'
}
}
else {
var m = parseInt(diff / MIN + '')
if (m >= 1) {
//是否相差超过1分钟
str += m + '分钟前'
}
else {
str += '刚刚'
}
}
}
}
}
}
return str
}
3. 转换数字为 大致数字描述
/**
* 转换数字为 大致数字描述
* @param value 数字:1001
* @returns 时间字符串:1千
*/
function makeChNumber(value) {
var newValue = ['', '', '']
var fr = 1000
var num = 3
var text1 = ''
var fm = 1
while (value / fr >= 1) {
fr *= 10
num += 1
}
if (num <= 4) {
// 千
newValue[0] = parseInt(value / 1000 + '') + ''
newValue[1] = '千'
}
else if (num <= 8) {
// 万
text1 = parseInt(num - 4 + '') / 3 > 1 ? '千万' : '万'
fm = text1 === '万' ? 10000 : 10000000
if (value % fm === 0) {
newValue[0] = parseInt(value / fm + '') + ''
}
else {
newValue[0] = parseFloat(value / fm + '').toFixed(1) + ''
}
newValue[1] = text1
}
else if (num <= 16) {
// 亿
text1 = (num - 8) / 3 > 1 ? '千亿' : '亿'
text1 = (num - 8) / 4 > 1 ? '万亿' : text1
text1 = (num - 8) / 7 > 1 ? '千万亿' : text1
fm = 1
if (text1 === '亿') {
fm = 100000000
}
else if (text1 === '千亿') {
fm = 100000000000
}
else if (text1 === '万亿') {
fm = 1000000000000
}
else if (text1 === '千万亿') {
fm = 1000000000000000
}
if (value % fm === 0) {
newValue[0] = parseInt(value / fm + '') + ''
}
else {
newValue[0] = parseFloat(value / fm + '').toFixed(2) + ''
}
newValue[1] = text1
}
if (value < 1000) {
newValue[0] = value + ''
newValue[1] = ''
}
return newValue.join('')
}
4. 判断xx相关工具函数
function isArray(input) {
return input instanceof Array || Object.prototype.toString.call(input) === '[object Array]';
}
function isEmpty(input) {
return typeof input === 'undefined' || input === null || input === '';
}
function isNumber(input) {
return input instanceof Number || Object.prototype.toString.call(input) === '[object Number]';
}
5. 浏览器存储 工具函数
function setLocal(name, content) {
if (!name)
return;
if (typeof content !== 'string') {
content = JSON.stringify(content);
}
if (window.localStorage) {
window.localStorage.setItem(name, content);
}
}
function getLocal(name) {
if (!name)
return null;
if (!window.localStorage)
return null;
return window.localStorage.getItem(name);
}
function removeLocal(name) {
if (!name)
return;
if (!window.localStorage)
return;
window.localStorage.removeItem(name);
}
function setSession(name, content) {
if (!name)
return;
if (typeof content !== 'string') {
content = JSON.stringify(content);
}
window.sessionStorage.setItem(name, content);
}
function getSession(name) {
if (!name)
return null;
return window.sessionStorage.getItem(name);
}
function removeSession(name) {
if (!name)
return;
window.sessionStorage.removeItem(name);
}