当前位置:网站首页 > Vue.js开发 > 正文

vue中,格式化时间戳转换成标准时间 & 获取随机id-uuid & random-m到n的随机数

vue中,格式化时间戳转换成标准时间 & 获取随机id-uuid & random-m到n的随机数

src\utils\index.ts

/ * @description 格式化时间 * @param time * @param cFormat * @returns {string|null} */ export function parseTime(time: string | number | Date, cFormat: string) { 
    if (arguments.length === 0) { 
    return null } const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}' let date: 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: any = { 
    y: date.getFullYear(), m: date.getMonth() + 1, d: date.getDate(), h: date.getHours(), i: date.getMinutes(), s: date.getSeconds(), a: date.getDay(), } return format.replace( /{([ymdhisa])+}/g, (result: string | any[], key: string) => { 
    let value = formatObj[key] if (key === 'a') { 
    return ['日', '一', '二', '三', '四', '五', '六'][value] } if (result.length > 0 && value < 10) { 
    value = '0' + value } return value || 0 } ) } / * @description 格式化时间 * @param time * @param option * @returns {string} */ export function formatTime(time: any | number | Date, option: any) { 
    if (('' + time).length === 10) { 
    time = parseInt(time) * 1000 } else { 
    time = +time } const d: any = new Date(time) const now = Date.now() const diff = (now - d) / 1000 if (diff < 30) { 
    return '刚刚' } else if (diff < 3600) { 
    // less 1 hour return Math.ceil(diff / 60) + '分钟前' } else if (diff < 3600 * 24) { 
    return Math.ceil(diff / 3600) + '小时前' } else if (diff < 3600 * 24 * 2) { 
    return '1天前' } if (option) { 
    return parseTime(time, option) } else { 
    return ( d.getMonth() + 1 + '月' + d.getDate() + '日' + d.getHours() + '时' + d.getMinutes() + '分' ) } } / * @description 将url请求参数转为json格式 * @param url * @returns { 
   {}|any} */ export function paramObj(url: string) { 
    const search = url.split('?')[1] if (!search) { 
    return { 
   } } return JSON.parse( '{"' + decodeURIComponent(search) .replace(/"/g, '\\"') .replace(/&/g, '","') .replace(/=/g, '":"') .replace(/\+/g, ' ') + '"}' ) } / * @description 父子关系的数组转换成树形结构数据 * @param data * @returns {*} */ export function translateDataToTree(data: any[]) { 
    const parent = data.filter( (value: { 
    parentId: string | null }) => value.parentId === 'undefined' || value.parentId === null ) const children = data.filter( (value: { 
    parentId: string | null }) => value.parentId !== 'undefined' && value.parentId !== null ) const translator = (parent: any[], children: any[]) => { 
    parent.forEach((parent: { 
    id: any; children: any[] }) => { 
    children.forEach((current: { 
    parentId: any }, index: any) => { 
    if (current.parentId === parent.id) { 
    const temp = JSON.parse(JSON.stringify(children)) temp.splice(index, 1) translator([current], temp) typeof parent.children !== 'undefined' ? parent.children.push(current) : (parent.children = [current]) } }) }) } translator(parent, children) return parent } / * @description 树形结构数据转换成父子关系的数组 * @param data * @returns {[]} */ export function translateTreeToData(data: any[]) { 
    const result: { 
    id: any; name: any; parentId: any }[] = [] data.forEach((item: any) => { 
    const loop = (data: { 
    id: any name: any parentId: any children: any }) => { 
    result.push({ 
    id: data.id, name: data.name, parentId: data.parentId, }) const child = data.children if (child) { 
    for (let i = 0; i < child.length; i++) { 
    loop(child[i]) } } } loop(item) }) return result } / * @description 10位时间戳转换 * @param time * @returns {string} */ export function tenBitTimestamp(time: number) { 
    const date = new Date(time * 1000) const y = date.getFullYear() let m: any = date.getMonth() + 1 m = m < 10 ? '' + m : m let d: any = date.getDate() d = d < 10 ? '' + d : d let h: any = date.getHours() h = h < 10 ? '0' + h : h let minute: any = date.getMinutes() let second: any = date.getSeconds() minute = minute < 10 ? '0' + minute : minute second = second < 10 ? '0' + second : second return y + '年' + m + '月' + d + '日 ' + h + ':' + minute + ':' + second //组合 } / * @description 13位时间戳转换 * @param time * @returns {string} */ export function thirteenBitTimestamp(time: number) { 
    const date = new Date(time / 1) const y = date.getFullYear() let m: any = date.getMonth() + 1 m = m < 10 ? '' + m : m let d: any = date.getDate() d = d < 10 ? '' + d : d let h: any = date.getHours() h = h < 10 ? '0' + h : h let minute: any = date.getMinutes() let second: any = date.getSeconds() minute = minute < 10 ? '0' + minute : minute second = second < 10 ? '0' + second : second return y + '年' + m + '月' + d + '日 ' + h + ':' + minute + ':' + second //组合 } / * @description 获取随机id * @param length * @returns {string} */ export function uuid(length = 32) { 
    const num = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' let str = '' for (let i = 0; i < length; i++) { 
    str += num.charAt(Math.floor(Math.random() * num.length)) } return str } / * @description m到n的随机数 * @param m * @param n * @returns {number} */ export function random(m: number, n: number) { 
    return Math.floor(Math.random() * (m - n) + n) } / * @description 数组打乱 * @param array * @returns {*} */ export function shuffle(array: any[]) { 
    let m = array.length, t: any, i: number while (m) { 
    i = Math.floor(Math.random() * m--) t = array[m] array[m] = array[i] array[i] = t } return array } / * @param data 源数据Tree类型 * @param originArr 需要改的字段 * @param targetKeyArr 获取的字段 */ export function recursionUpdateName( data: any[], originArr: string[], targetKeyArr: string[] ) { 
    if (!Array.isArray(data)) return [] function fn(data: any[]) { 
    return data.map((item: { 
    [x: string]: any; children: any[] }) => { 
    originArr.forEach((key, idx) => { 
    console.log(key) console.log(targetKeyArr[idx]) item[key] = item[targetKeyArr[idx]] }) if (item.children && item.children.length) { 
    fn(item.children) } return item }) } return fn(data) } 
到此这篇vue中,格式化时间戳转换成标准时间 & 获取随机id-uuid & random-m到n的随机数的文章就介绍到这了,更多相关内容请继续浏览下面的相关推荐文章,希望大家都能在编程的领域有一番成就!

版权声明


相关文章:

  • js计算两个时间戳的时间差之多少天、时、分、秒,格式0000、动态显示年月日时分秒(不足两位补0)2024-12-03 10:09:05
  • vue项目中 jsconfig.json和tsconfig.json文件配置释义 & compilerOptions配置2024-12-03 10:09:05
  • vue中,js封装方法之判断js对象类型 & 封装方法之js对象深拷贝2024-12-03 10:09:05
  • js之对象数组操作——添加到数组中、删除非同对象、非同对象参数置空2024-12-03 10:09:05
  • js中,删除arr1中比arr2中多的对象之filter、find & 数组中是否有相同对象之every、some & 删除数组中不是相同的对象 & 对象数组,去重后合并2024-12-03 10:09:05
  • vue3中,父子组件props传函数写法-传动态接口 & 特殊字符校验 & 封装公共input组件2024-12-03 10:09:05
  • vue3项目初始化配置流程(含注释)2024-12-03 10:09:05
  • vue3中,全局自定义指令——DirectiveBinding & v-throttle节流-保存和提交按钮时间间隔内点击多次只执行一次2024-12-03 10:09:05
  • vue常见源码面试题2024-12-03 10:09:05
  • npm相关之npm初始化、切换npm镜像源、package.json释义、require 的加载机制 & ES6降级处理-babel包2024-12-03 10:09:05
  • 全屏图片