前端常用方法分享, 用户输入钱, 转化成分, 类型判断, 时间格式化

发布时间 2023-09-06 11:07:42作者: 深海里的星星i

钱转化成分

        // 转换成分
        function toCent(numStr) {
            const reg = /(^(?=.*?[1-9].*?)0\.\d+$)|(^[1-9][0-9]*(\.\d{1,2})?$)/
            if (!reg.test(numStr)) {
                throw new Error('input error. eg: 0.5 or 6 or 1.1 or 0.03')
                return 0
            }
            // 开始转换
            const index = numStr.indexOf('.')
            if (index < 0) {
                // 输入的是整数
                return parseInt(numStr * 100)
            }
            const tempArr = numStr.split('.')
            const integer = parseInt(tempArr[0] * 100)
            const cent = parseInt(tempArr[1].padEnd(2, '0'))
            return integer + cent
        }
        console.log(toCent('0.5'))  // 50
        console.log(toCent('6'))  // 600
        console.log(toCent('1.1')) // 110
        console.log(toCent('0.03')) // 3

 

获取任意类型的类型

        function type(any) {
            const typeStr = Object.prototype.toString.call(any)
            const reg = /\s[a-zA-Z]+/g
            return typeStr.match(reg)[0].replace(/\s/g, '')
        }
        console.log(type(null))  // Null
        console.log(type(3.14)) // Number
        console.log(type(520)) // Number
        console.log(type('hello world')) // String
        console.log(type([1, 2])) // Array
        console.log(type({ sex: '男' })) // Object
        console.log(type(undefined)) // Undefined
        console.log(type(NaN))  // Number  (比较特殊)
        console.log(type(true))  // Boolean
        console.log(type(() => { }))  // Function

        class A { }
        console.log(type(A))  // Function

 

时间格式转化

        function fmt(opt = {}) {
            const { str = 'YYYY-MM-DD hh:mm:ss', date = new Date(), cp = 0 } = opt
            const t = date.toString().replace(/-/g, '/')
            const nt = +new Date(t) + cp * 24 * 60 * 60 * 1000
            const YYYY = new Date(nt).getFullYear()
            const MM = (new Date(nt).getMonth() + 1 + '').padStart(2, '0')
            const DD = (new Date(nt).getDate() + '').padStart(2, '0')
            const hh = (new Date(nt).getHours() + '').padStart(2, '0')
            const mm = (new Date(nt).getMinutes() + '').padStart(2, '0')
            const ss = (new Date(nt).getSeconds() + '').padStart(2, '0')
            return str.replace('YYYY', YYYY).replace('MM', MM).replace('DD', DD).replace('hh', hh).replace('mm', mm).replace('ss', ss)
        }
        console.log(fmt())  // 2023-09-06 10:56:09
        console.log(fmt({ str: 'YYYY-MM-DD' }))  // 2023-09-06
        console.log(fmt({ str: 'MM-DD hh:mm' }))  // 09-06 10:56
        console.log(fmt({ date: '2020-12-12' }))  // 2020-12-12 00:00:00
        console.log(fmt({ date: '2020-12-12', cp: 365 }))  // 2021-12-12 00:00:00
        console.log(new Date())  // Wed Sep 06 2023 10:58:20 GMT+0800 (中国标准时间)
        console.log(fmt({ cp: -1 }))  // 2023-09-05 10:58:20

 

 

获取某一个月有多少天

语法要点: new Date(2023, 2, 0)  // 28

        // 获取一个月有多少天  2023-08
        function getDays(str) {
            const reg = /^\d{4}-\d{2}$/
            if (!reg.test(str)) {
                throw new Error('getDays function need params format YYYY-MM eg: 2023-08')
                return
            }
            const yyyy = str.split('-')[0] / 1
            const mm = str.split('-')[1] / 1
            return new Date(yyyy, mm, 0).getDate()
        }

        const days = getDays('2023-02')
        console.log(days) // 28