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;
}
}