utils

作者: 小米和豆豆 | 来源:发表于2021-09-26 09:25 被阅读0次
  1. date.js
/*
 * @Author: DDY
 * @Date: 2021-09-05 18:32:32
 */

/**
 * @description: 格式化日期
 * @param {*} formatStr 日期格式
 */
Date.prototype.Format = function (formatStr) {
    var str = formatStr;
    var Week = ['日', '一', '二', '三', '四', '五', '六'];
    str = str.replace(/yyyy|YYYY/, this.getFullYear());
    str = str.replace(/yy|YY/, (this.getYear() % 100) > 9 ? (this.getYear() % 100).toString() : '0' + (this.getYear() % 100));
    var month = this.getMonth() + 1;
    str = str.replace(/MM/, month > 9 ? month.toString() : '0' + month);
    str = str.replace(/M/g, month);

    str = str.replace(/w|W/g, Week[this.getDay()]);

    str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate().toString() : '0' + this.getDate());
    str = str.replace(/d|D/g, this.getDate());

    str = str.replace(/hh|HH/, this.getHours() > 9 ? this.getHours().toString() : '0' + this.getHours());
    str = str.replace(/h|H/g, this.getHours());
    str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : '0' + this.getMinutes());
    str = str.replace(/m/g, this.getMinutes());

    str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds().toString() : '0' + this.getSeconds());
    str = str.replace(/s|S/g, this.getSeconds());
    return str;
}
console.log(new Date().Format("yyyy/MM/dd/星期w hh:mm:ss"));
  1. session.js
/*
 * @Author: DDY
 * @Date: 2021-09-05 15:12:43
 */
const mySession ={
    get:(key)=>{
        try{
            const value = sessionStorage.getItem(key);
            if (value === null || value === undefined || value === "") {
                return null;
            }
             return JSON.parse(value)

        }
        catch(err){
            return null
        }
    },
    set:(key,val)=>{
        sessionStorage.setItem(key,JSON.stringify(val))
    },
    remove:(key)=>{
        sessionStorage.removeItem(key)
    },
    clear:(key)=>{
        sessionStorage.clear()
    }
}
const myStorage ={
    //取出数据
    get:(key)=>{
        try {
            const value = localStorage.getItem(key);
            if (value === null || value === undefined || value === "") {
                return null;
            }
            return JSON.parse(value);
        } catch (err) {
            return null
        }
    },
    //存储
    set: (key, value) => {
        localStorage.setItem(key, JSON.stringify(value));
    },
    // 删除数据
    remove:(key)=> {
        localStorage.removeItem(key);
    },
    clear:()=>{
        localStorage.clear()
    }
}
export {
    mySession,
    myStorage
}
  1. require.js
//见 require.js

相关文章

网友评论

      本文标题:utils

      本文链接:https://www.haomeiwen.com/subject/cayvvltx.html