常用js轉換
阿新 • • 發佈:2020-09-18
獲取當前日期字串/^1[3456789]\d{9}$/ exportfunctioncheckMobile(mobile){ mobile=mobile.toString(); varindex1=mobile.substring(0,1); varindex2=mobile.substring(0,2); if(index2=='11'||index2=='12'||index1!=='1'||mobile.length!==11){ returnfalse; }else{ returntrue; } }
function formatDateTime() { var date = new Date(); var y = date.getFullYear(); var m = date.getMonth() + 1; m = m < 10 ? ('0' + m) : m; var d = date.getDate(); d = d < 10 ? ('0' + d) : d; var h = date.getHours(); h = h < 10 ? ('0' + h) : h; var minute = date.getMinutes();var second = date.getSeconds(); minute = minute < 10 ? ('0' + minute) : minute; second = second < 10 ? ('0' + second) : second; return{ 'ymd':y + '-' + m + '-' + d, 'ymdhm':y + '-' + m + '-' + d + ' ' + h + ':' + minute, } }
//base64轉成二進位制 exportfunctiondataURItoBlob(dataurl){ vararr=dataurl.split(',') //注意base64的最後面中括號和引號是不轉譯的 var_arr=arr[1].substring(0,arr[1].length-2) varmime=arr[0].match(/:(.*?);/)[1] varbstr=atob(_arr) varn=bstr.length varu8arr=newUint8Array(n) while(n--){ u8arr[n]=bstr.charCodeAt(n) } returnnewBlob([u8arr],{ type:mime }) }
//圖片Url轉成Base64 exportfunctionconvertImgToBase64(url,callback,outputFormat){ varcanvas=document.createElement('CANVAS') varctx=canvas.getContext('2d') varimg=newImage() img.crossOrigin='Anonymous' img.onload=function(){ canvas.height=img.height canvas.width=img.width ctx.drawImage(img,0,0) vardataURL=canvas.toDataURL(outputFormat||'image/png') callback.call(this,dataURL) canvas=null } img.src=url }
//base64轉file物件 exportfunctiondataURLtoFile(dataURL,fileName,fileType){ vararr=dataURL.split(',');varmime=arr[0].match(/:(.*?);/)[1] varbstr=atob(arr[1]);varn=bstr.length;varu8arr=newUint8Array(n) while(n--){ u8arr[n]=bstr.charCodeAt(n) } returnnewFile([u8arr],fileName,{type:fileType||'image/jpg'}) }
storage.js
varstorage={
set(key,value){
localStorage.setItem(key,JSON.stringify(value))
},
get(key){
returnlocalStorage.getItem(key)
},
remove(key){
localStorage.removeItem(key)
},
clear(){
localStorage.clear()
}
}
exportdefaultstorage
exportfunctionparseTime(time,cFormat){
if(arguments.length===0||!time){
returnnull
}
constformat=cFormat||'{y}-{m}-{d}{h}:{i}:{s}'
letdate
if(typeoftime==='object'){
date=time
}else{
if((typeoftime==='string')){
if((/^[0-9]+$/.test(time))){
//support"1548221490638"
time=parseInt(time)
}else{
//supportsafari
//https://stackoverflow.com/questions/4310953/invalid-date-in-safari
time=time.replace(newRegExp(/-/gm),'/')
}
}
if((typeoftime==='number')&&(time.toString().length===10)){ time=time*1000 } date=newDate(time) } constformatObj={ y:date.getFullYear(), m:date.getMonth()+1, d:date.getDate(), h:date.getHours(), i:date.getMinutes(), s:date.getSeconds(), a:date.getDay() } consttime_str=format.replace(/{([ymdhisa])+}/g,(result,key)=>{ constvalue=formatObj[key] //Note:getDay()returns0onSunday if(key==='a'){return['日','一','二','三','四','五','六'][value]} returnvalue.toString().padStart(2,'0') }) returntime_str } exportfunctionformatTime(time,option){ if((''+time).length===10){ time=parseInt(time)*1000 }else{ time=+time } constd=newDate(time) constnow=Date.now()
constdiff=(now-d)/1000
if(diff<30){ return'剛剛' }elseif(diff<3600){ //less1hour returnMath.ceil(diff/60)+'分鐘前' }elseif(diff<3600*24){ returnMath.ceil(diff/3600)+'小時前' }elseif(diff<3600*24*2){ return'1天前' } if(option){ returnparseTime(time,option) }else{ return( d.getMonth()+ 1+ '月'+ d.getDate()+ '日'+ d.getHours()+ '時'+ d.getMinutes()+ '分' ) } }