1、时间类方法
1.1 时间格式化为yyyy-MM-dd h:i:s
- 标准时间格式化为yyyy-MM-dd h:i:s格式的时间
export function fullformatDate(cellValue) {
if (cellValue == null || cellValue == '') return ''
var date = new Date(cellValue)
var year = date.getFullYear()
var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
var hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
var minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
var seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
}
1.2 时间格式化为yyyy-MM-dd
export function formatDate(cellValue) {
if (cellValue == null || cellValue == '') return ''
var date = new Date(cellValue)
var year = date.getFullYear()
var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
var hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
var minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
var seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
return year + '-' + month + '-' + day
}
1.3 标准时间"数组"格式化为yyyy-MM-dd
- 标准时间"数组"格式化为yyyy-MM-dd格式的时间"数组"
export const date_handle = mon_arr => {
if (mon_arr) {
var res_arr = []
mon_arr.forEach(item => {
var d = new Date(item)
var datetime = ''
if (d.getMonth() < 9) {
if (d.getDate() < 10) {
datetime = d.getFullYear() + '-0' + (d.getMonth() + 1) + '-0' + d.getDate()
} else {
datetime = d.getFullYear() + '-0' + (d.getMonth() + 1) + '-' + d.getDate()
}
} else {
if (d.getDate() < 10) {
datetime = d.getFullYear() + '-' + (d.getMonth() + 1) + '-0' + d.getDate()
} else {
datetime = d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate()
}
}
res_arr.push(datetime)
})
}
return res_arr
}
1.4 时间段:6小时/月初到现在/年初到现在
- 获取截止到此刻为止的:6小时的时间段/本月初到现在的时间段/今年年初到现在的时间段
export const time_compute = type => {
let time = ''
let _today = ''
let _month = ''
let _year = ''
let d = new Date()
let year = d.getFullYear()
let mon = d.getMonth()+1
let day = d.getDate()
let hour = d.getHours()
let min = d.getMinutes()
let _now = hour>9?(hour+':'):('0'+hour+':')
_now = _now + (min>9?(min):('0'+min))
let _six = hour<6?(24-(6-hour)):(hour-6)
_six = _six>9?(_six+':'):('0'+_six+':')
_six = _six + (min>9?(min):('0'+min))
_today = _six + '至' + _now
let _now_mon = mon>9?(year+'-'+mon):(year+'-0'+mon)
let first_mon = _now_mon + '-01'
let temp_day = day>9?('-'+day):('-0'+day)
_now_mon = _now_mon + temp_day
_month = day===1?_now_mon:(first_mon + '至' + _now_mon)
let _now_y = mon>9?(year+'-'+mon):(year+'-0'+mon)
let first_y = year + '-01'
_year = mon===1?_now_y:(first_y + '至' + _now_y)
switch(type) {
case '6小时':
time = _today
break
case '日':
time = _month
break
case '月':
time = _year
break
}
return time
}
1.5 标准时间"数组"格式化为yyyy-MM
- 用于将获取的中国标准时间数组转换成yyyy-mm的格式
export const month_handle = mon_arr => {
if (mon_arr) {
var res_arr = []
mon_arr.forEach(item => {
var d = new Date(item)
var datetime = ''
if (d.getMonth() < 9) {
datetime = d.getFullYear() + '-0' + (d.getMonth() + 1)
} else {
datetime = d.getFullYear() + '-' + (d.getMonth() + 1)
}
res_arr.push(datetime)
})
}
return res_arr
}
1.6 标准时间"数组"格式化为yyyy-MM
- 用于获取当前日期以及一个月前的日期yyyy-mm-dd格式[‘2021-01-17’, ‘2020-12-17’]
export const onemonth_to_now = () => {
var today = ''
var one_month = ''
var res = []
var d = new Date()
var m = d.getMonth()
var date = d.getDate()
var m_text = ''
var date_text = ''
if(m===0) {
if (date < 10) {
date_text = '-0' + date
} else {
date_text = '-' + date
}
one_month = (d.getFullYear()-1) + '-12' + date_text
} else {
if (m < 10) {
m_text = '-0' + m
} else {
m_text = '-' + m
}
if (date < 10) {
date_text = '-0' + date
} else {
date_text = '-' + date
}
one_month = d.getFullYear() + m_text + date_text
}
m = d.getMonth() + 1
if (m < 10) {
m_text = '-0' + m
} else {
m_text = '-' + m
}
today = d.getFullYear() + m_text + date_text
res.push(today)
res.push(one_month)
return res
}
1.7 时间格式化
export function parseTime (time, cFormat) {
if (arguments.length === 0) {
return null
}
const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
let date
if (typeof time === 'object') {
date = time
} else {
if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
time = parseInt(time)
}
if ((typeof time === 'number') && (time.toString().length === 10)) {
time = time * 1000
}
date = new Date(time)
}
const formatObj = {
y: date.getFullYear(),
m: date.getMonth() + 1,
d: date.getDate(),
h: date.getHours(),
i: date.getMinutes(),
s: date.getSeconds(),
a: date.getDay()
}
const timeStr = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
let value = formatObj[key]
if (key === 'a') {
return ['日', '一', '二', '三', '四', '五', '六'][value] }
if (result.length > 0 && value < 10) {
value = '0' + value
}
return value || 0
})
return timeStr
}
1.8 截止到本月所有月份时间
- 用于获取截止到本月所有月份的 中国标准时间格式数组 和 yyyy-mm-dd格式数组
export const months_to_now = () => {
var arr_s = []
var arr_n = []
var d = new Date()
var datetime = ''
var s_datetime
var m = d.getMonth() + 1
for (let i = m; i > 0; i--) {
if (i < 10) {
datetime = d.getFullYear() + '-0' + i + '-01'
} else {
datetime = d.getFullYear() + '-' + i + '-01'
}
s_datetime = new Date(datetime + ' ')
arr_s.push(s_datetime)
arr_n.push(datetime)
}
return {
arr_s: arr_s, arr_n: arr_n }
}
1.9 本月初的yyyy-mm-dd
- 用于将获取yyyy-mm-dd格式的当前时间,且是1号
export const month_now = () => {
var d = new Date()
var datetime = ''
if (d.getMonth() < 9) {
datetime = d.getFullYear() + '-0' + (d.getMonth() + 1) + '-01'
} else {
datetime = d.getFullYear() + '-' + (d.getMonth() + 1) + '-01'
}
return datetime
}
1.10 月份处理为X月
export const mdata_handle = data => {
if (data && data.length > 0) {
for (var i = 0; i < data.length; i++) {
var val = data[i].date
if (val && val.length > 5) {
data[i].date = val.split('-')[1] + '月'
}
}
return data
}
}
1.11 时间段数组:月初到现在
- 用于将获取截止到此刻为止的:本月初到现在的时间段([‘2021-01-01’, ‘2021-01-17’])
export const this_month = () => {
let _month = []
let d = new Date()
let year = d.getFullYear()
let mon = d.getMonth()+1
let day = d.getDate()
let _now_mon = mon>9?(year+'-'+mon):(year+'-0'+mon)
let first_mon = _now_mon + '-01'
let temp_day = day>9?('-'+day):('-0'+day)
_now_mon = _now_mon + temp_day
_month.push(first_mon, _now_mon)
return _month
}
1.12 时间段数组:年初到现在
- 用于将获取截止到此刻为止的:年初初到现在的时间段([‘2021-01’, ‘2021-07’])
export const this_year = () => {
let _month = []
let d = new Date()
let year = d.getFullYear()
let mon = d.getMonth()+1
let _now_mon = mon>9?(year+'-'+mon):(year+'-0'+mon)
let first_mon = year+'-01'
_month.push(first_mon, _now_mon)
return _month
}
1.13 时间段数组:一周
- 用于将获取截止到此刻为止的:一周前到现在的时间段([‘2021-01-11’, ‘2021-01-17’])
export const this_week = () => {
let res = []
let _now = new Date
_now=_now.getFullYear() + "-" + (_now.getMonth()> 9 ? (_now.getMonth() + 1) : "0" + (_now.getMonth() + 1)) + "-" +(_now.getDate()> 9 ? (_now.getDate()) : "0" + (_now.getDate()));
let time=(new Date).getTime()-7*24*60*60*1000;
let one_week=new Date(time);
one_week=one_week.getFullYear() + "-" + (one_week.getMonth()> 9 ? (one_week.getMonth() + 1) : "0" + (one_week.getMonth() + 1)) + "-" +(one_week.getDate()> 9 ? (one_week.getDate()) : "0" + (one_week.getDate()));
res.push(one_week, _now)
return res;
}
2、计算类
2.1 加
export function add (a, b) {
let c
let d
let e
try {
c = a.toString().split('.')[1].length
} catch (f) {
c = 0
}
try {
d = b.toString().split('.')[1].length
} catch (f) {
d = 0
}
return (e = Math.pow(10, Math.max(c, d)), (mul(a, e) + mul(b, e)) / e)
}
2.2 减
export function sub (a, b) {
let c
let d
let e
try {
c = a.toString().split('.')[1].length
} catch (f) {
c = 0
}
try {
d = b.toString().split('.')[1].length
} catch (f) {
d = 0
}
return (e = Math.pow(10, Math.max(c, d)), (mul(a, e) - mul(b, e)) / e)
}
2.3 乘
export function mul (a, b) {
let c = 0
const d = a.toString()
const e = b.toString()
try {
c += d.split('.')[1].length
} catch (f) {
}
try {
c += e.split('.')[1].length
} catch (f) {
}
return Number(d.replace('.', '')) * Number(e.replace('.', '')) / Math.pow(10, c)
}
2.4 除
export function div (a, b) {
let c
let d
let e = 0
let f = 0
try {
e = a.toString().split('.')[1].length
} catch (g) {
}
try {
f = b.toString().split('.')[1].length
} catch (g) {
}
return (c = Number(a.toString().replace('.', '')), d = Number(b.toString().replace('.', '')), mul(c / d, Math.pow(10, f - e)))
}
3、通用类
3.1 深度拷贝
export function deepCopy (data) {
const t = isArray(data) ? 'array' : typeof data
let o
if (t === 'array') {
o = []
} else if (t === 'object') {
o = {
}
} else {
return data
}
if (t === 'array') {
for (let i = 0; i < data.length; i++) {
o.push(deepCopy(data[i]))
}
} else if (t === 'object') {
for (const i in data) {
o[i] = deepCopy(data[i])
}
}
return o
}
3.2 判断arr是否为数组
export function isArray (arr) {
if (!Array.isArray) {
Array.isArray = (arg) => {
return Object.prototype.toString.call(arg) === '[object Array]'
}
}
return Array.isArray(arr)
}
3.3 数组去重
export function unique (arr) {
if (Array.from(new Set(arr))) {
return Array.from(new Set(arr))
} else {
for (var i = 0; i < arr.length; i++) {
for (var j = i + 1; j < arr.length; j++) {
if (arr[i] === arr[j]) {
arr.splice(j, 1)
j--
}
}
}
return arr
}
}
3.4 数组元素字符串转数字
export const atrToNum_handle = arr => {
if (arr && arr.length > 0) {
for (var i = 0; i < arr.length; i++) {
arr[i] = Number(arr[i])
}
return arr
}
}
3.5 post方式的window.open请求
export const openPostWindow = (url, params) => {
const newWin = window.open(); let formStr = ''
formStr = '<form style="visibility:hidden;" method="POST" target="_blank" action="' + url + '">' +
'<input type="hidden" name="params" value="' + params + '" />' +
'</form>'
newWin.document.body.innerHTML = formStr
newWin.document.forms[0].submit()
return newWin
}
3.6 生成唯一id
export const genID = length => {
return Number(Math.random().toString().substr(3, length) + Date.now()).toString(36)
}
4、组件相关
4.1 nToN组件初始数据处理
export function init_range_handle (init_range) {
if (!init_range.minNum) {
const res_range = {
minNum: '',
maxNum: ''
}
const {
min, max } = init_range
if (min === '不限' && max === '不限') {
res_range.minNum = ''
res_range.maxNum = ''
} else if (min !== '不限' && max === '不限') {
res_range.minNum = min
res_range.maxNum = ''
} else {
res_range.minNum = min || ''
res_range.maxNum = max || ''
}
return res_range
}
return init_range
}
4.2 c-ntn组件初始数据处理
export function c_init_range_handle (init_range) {
const res_range = {
min: null,
max: null
}
const {
min, max } = init_range
if (min === '不限' && max === '不限') {
res_range.min = null
res_range.max = null
} else if (min !== '不限' && max === '不限') {
res_range.min = Number(min)
res_range.max = null
} else {
res_range.min = min === 0 || min ? Number(min) : null
res_range.max = max === 0 || max ? Number(max) : null
}
return res_range
}
4.3 待提交数据处理
export function commit_range_handle (init_range) {
if (!init_range.min) {
const res_range = {
min: '',
max: ''
}
const {
minNum, maxNum } = init_range
if (minNum === '' && maxNum === '') {
res_range.min = '不限'
res_range.max = '不限'
} else if (minNum !== '' && maxNum === '') {
res_range.min = minNum
res_range.max = '不限'
} else if (minNum === '' && maxNum !== '') {
res_range.min = 0
res_range.max = 'maxNum'
} else {
res_range.min = minNum || ''
res_range.max = maxNum || ''
}
return res_range
}
return init_range
}
4.4 addcondition中c-ntn待提交数据处理
export function c_commit_range_handle (init_range) {
const res_range = {
min: null,
max: ''
}
const {
min, max } = init_range
if (typeof min === 'object' && typeof max === 'object') {
res_range.min = '不限'
res_range.max = '不限'
} else if (typeof min !== 'object' && typeof max === 'object') {
res_range.min = min.toString()
res_range.max = '不限'
} else if (typeof min === 'object' && typeof max !== 'object') {
res_range.min = '0'
res_range.max = '不限'
} else {
res_range.min = min.toString()
res_range.max = max.toString()
}
return res_range
}
4.5 ntn组件抛出数据处理
export function ntnData_handle (data) {
var res = {
content: '',
range: {
min: '',
max: ''
}
}
var content = ''
var left = data[0].toString()
var right = data[1].toString()
if (left === right && left !== '') {
content = '=' + left
} else if (left === '' && right === '') {
left = '不限'
right = '不限'
content = '不限'
} else if (left === '' && right !== '') {
left = '0'
content = '[' + left + ',' + right + ')'
} else if (left !== '' && right === '') {
right = '不限'
content = '[' + left + ',' + right + ')'
} else {
content = '[' + left + ',' + right + ')'
}
res.content = content
res.range.min = left
res.range.max = right
return res
}
4.5 c-ntn组件抛出数据处理
export function cntnData_handle (data) {
var res = {
content: '',
range: {
min: '',
max: ''
}
}
var content = ''
var left = data[0] === null ? null : data[0].toString()
var right = data[1] === null ? null : data[1].toString()
if (left === right && left !== null) {
content = '=' + left
} else if (left === null && right === null) {
left = '不限'
right = '不限'
content = '不限'
} else if (left === null && right !== null) {
left = '0'
content = '[' + left + ',' + right + ')'
} else if (left !== null && right === null) {
right = '不限'
content = '[' + left + ',' + right + ')'
} else {
content = '[' + left + ',' + right + ')'
}
res.content = content
res.range.min = left
res.range.max = right
return res
}
5、杂乱
5.1 element表格合并列处理
export const d_merge_handle = (data, attr, dep_attr) => {
if (data) {
var rowspans = []
if (dep_attr) {
var d_value = ''
var data_arr = []
var data_arr_pos = 0
for (let i = 0; i < data.length; i++) {
if (i === 0) {
d_value = data[0][dep_attr]
data_arr[data_arr_pos] = []
data_arr[data_arr_pos].push(data[0])
} else {
if (data[i][dep_attr] === d_value) {
data_arr[data_arr_pos].push(data[i])
} else {
d_value = data[i][dep_attr]
data_arr_pos++
data_arr[data_arr_pos] = []
data_arr[data_arr_pos].push(data[i])
}
}
}
data_arr.forEach(arr_item => {
var temp_arr = merge_handle(arr_item, attr)
rowspans = [...rowspans, ...temp_arr]
})
} else {
rowspans = merge_handle(data, attr)
}
return rowspans
}
return []
}
5.2 element表格合并列处理
export const merge_handle = (data, attr) => {
if (data) {
var pos = 0
var rowspans = []
for (let i = 0; i < data.length; i++) {
if (i === 0) {
rowspans[i] = 1
pos = 0
} else {
if (data[i][attr] === data[i - 1][attr]) {
rowspans[pos] += 1
rowspans.push(0)
} else {
rowspans.push(1)
pos = i
}
}
}
return rowspans
}
return []
}
5.3 导出前处理
export const before_export = (obj, post_param) => {
if (obj && Object.keys(obj).length > 0) {
var res_arr = []
var keys_arr = Object.keys(obj)
var str = ''
keys_arr.forEach(item => {
if (typeof obj[item] === 'object') {
obj[item].forEach(i => {
str = item + '=' + i
res_arr.push(str)
})
} else if (typeof obj[item] === 'string') {
str = item + '=' + obj[item]
res_arr.push(str)
}
})
if (post_param) return res_arr.join(',')
return res_arr.join('&')
}
}