* @param {type} 类型
* type == 1 ---> "yyyy-mm-dd hh:MM:ss.fff"
* type == 2 ---> "yyyymmddhhMMss"
* type == '' ---> "yyyy-mm-dd hh:MM:ss"
*/
export const formatDate = (date, type) =>{
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 hour = 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();//秒
var milliseconds = date.getMilliseconds() < 10 ? "0" + date.getMilliseconds() : date.getMilliseconds() //毫秒
if (type == 1) {
return year + "-" + month + "-" + day + " " + hour + ":" + minutes + ":" + seconds + "." + milliseconds;
} else if(type == 2){
return year+""+month+""+day+""+hour+""+minutes+""+seconds;
}else if(type == 3){
return year + "-" + month + "-" + day;
}else {
return year + "-" + month + "-" + day + " " + hour + ":" + minutes + ":" + seconds;
}
}
/**
* 时间转换:20150101010101 --> '2015-01-01 01:01:01'
*/
export const parseToDate = (timeValue) => {
var result = ""
var year = timeValue.substr(0, 4)
var month = timeValue.substr(4, 2)
var date = timeValue.substr(6, 2)
var hour = timeValue.substr(8, 2)
var minute = timeValue.substr(10, 2)
var second = timeValue.substr(12, 2)
result = year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second
return result
}
export const toFixedNum = (num) => {
const tonum = Number(num).toFixed(2);
return tonum;
}
**数组列表转树形结构**
const arrayToTree = (items, childName = 'children') => {
const result = []
const itemMap = {}
for (const item of items) {
const id = item.id
const pid = item.parentId
if (!itemMap[id]) {
itemMap[id] = {
[childName]: [],
}
}
itemMap[id] = {
...item,
[childName]: itemMap[id][childName],
}
const treeItem = itemMap[id]
if (pid === 0) {
result.push(treeItem)
} else {
// if (!itemMap[pid]) {
// itemMap[pid] = {
// [childName]: [],
// }
// }
itemMap[pid][childName].push(treeItem)
}
}
return result
}
const treeToList = (data, childName = 'children') => {
// if (!Array.isArray(data)) {
// return []
// }
return data.reduce(
(prev, cur) =>
prev.concat([cur], treeToList(cur[childName] || [])),
[]
)
}