//日期转换
export function timeToDay(num,type=1){
let year = new Date(num).getFullYear()
let month = new Date(num).getMonth() + 1 > 9 ? new Date(num).getMonth() + 1 : `0${new Date(num).getMonth() + 1}`
let day = new Date(num).getDate() > 9 ? new Date(num).getDate() : `0${new Date(num).getDate()}`
let hou = new Date(num).getHours() > 9 ? new Date(num).getHours() : `0${new Date(num).getHours()}`
let min = new Date(num).getMinutes() > 9 ? new Date(num).getMinutes() : `0${new Date(num).getMinutes()}`
let sec = new Date(num).getSeconds() > 9 ? new Date(num).getSeconds() : `0${new Date(num).getSeconds()}`
if(type === 1){
return `${hou}:${min}:${sec}`
}else if(type == 2){
return `${year}/${month}/${day}`
}else{
return `${year}/${month}/${day} ${hou}:${min}:${sec}`
}
}
//数据流转换
// base64转blob
export function dataURLtoBlob(dataurl) {
var arr = dataurl.split(','),
mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]),
n = bstr.length,
u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([u8arr], { type: mime });
}
// blob转file
export function blobToFile(theBlob, fileName){
theBlob.lastModifiedDate = new Date(); // 文件最后的修改日期
theBlob.name = fileName; // 文件名
return new File([theBlob], fileName, {type: theBlob.type, lastModified: Date.now()});
}
// base64转file
export function base64ToFile(urlData, fileName) {
let arr = urlData.split(",");
let mime = arr[0].match(/:(.*?);/)[1];
let bytes = atob(arr[1]); // 解码base64
let n = bytes.length;
let ia = new Uint8Array(n);
while (n--) {
ia[n] = bytes.charCodeAt(n);
}
return new File([ia], `${this.imgData.bucketName}.png`, { type: mime });
}
// 图片file转Base64编码
export function imageToBase64 (file) {
var reader = new FileReader()
reader.readAsDataURL(file)
reader.onload = () => {
console.log('file 转 base64结果:' + reader.result)
this.iconBase64 = reader.result
}
reader.onerror = function (error) {
console.log('Error: ', error)
}
}
//16进制和2进制转换
//十六进制转换十进制
parseInt(num,16)
//十进制转换十六进制
num.toString(16)