ES6扩展方法

1、格式化时间
/* 保留两位小数
 * @method fixed2
 * @param {String} str 需要转换的字符串
 * @return {String} 返回两位小数
*/
const fixed2=(str)=>{
    return Number.parseInt(str)<10?'0'+str:str;
}

/* 格式化时间
 * @method fmtDate
 * @param {String} str 时间戳字符串
 * @param {String} d 日期格式,默认为时间格式
 * @return {String} 返回格式化之后的日期
*/
const fmtDate=(str,d)=>{
    let dt=new Date(str);
    let year=dt.getFullYear();
    let month=fixed2(dt.getMonth()+1);
    let date=fixed2(dt.getDate());
    let hours=fixed2(dt.getHours());
    let minutes=fixed2(dt.getMinutes());
    let secounds=fixed2(dt.getSeconds());
    let times='';
    if(d){
      times= `${year}-${month}-${date}`;
    }else{
      times= `${year}-${month}-${date} ${hours}:${minutes}:${secounds}`;
    }
    return times;
}
2、浏览器缓存的增删改查
class Cache {
	// 获取临时存储
	getSession:function(name){
        if(!name) return
        return window.sessionStorage.getItem(name)
    }    
    
    // 设置临时存储
    setSession:function(name,content){
        if(!name) return
        if(typeof content !=='string'){
            content=JSON.stringify(content)
        }
        window.sessionStorage.setItem(name,content)
    }    
    
    // 移除临时存储
    removeSession:function(name){
        if(!name) return
        window.sessionStorage.removeItem(name)
    }
    
    // 获取永久存储
    getLocal:function(name){
        if(!name) return
        return window.localStorage.getItem(name)
    }
    
    // 设置永久存储
    setLocal:function(name,content){
        if(!name) return
        if(typeof content!=='string'){
            content=JSON.stringify(content)
        }
        window.localStorage.setItem(name,content)
    }
    
    // 移除永久存储
    removeLocal:function(name){
        if(!name) return
        window.localStorage.removeItem(name)
    }
}

const cacheFn =new Cache();
3、对象字符串转成对象
/* 对象字符串转成对象
 * @method strToArr
 * @param {String} str 需要转换的字符串
 * @return {Object} 返回转换后的对象
*/
const strToArr=(str)=>{
    if(str){
        let strData=JSON.parse(str);
        strData=typeof(strData)=='string'?JSON.parse(strData):strData;
        return strData;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值