時間格式轉(zhuǎn)換
const transFormation = {
// 獲取當前日期
getLocalTime() {
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
const hour = date.getHours();
const mins = date.getMinutes();
const seconds = date.getSeconds();
const time = `${year}-${transFormation.addZero(month)}-${transFormation.addZero(day)} ${transFormation.addZero(hour)}:${transFormation.addZero(mins)}:${transFormation.addZero(seconds)}`;
return time;
},
// 獲取一周前日期
getWeekAgo() {
const now = new Date();
const date = new Date(now.getTime() - 7 * 24 * 3600 * 1000);
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
const hour = date.getHours();
const mins = date.getMinutes();
const seconds = date.getSeconds();
const time = `${year}-${transFormation.addZero(month)}-${transFormation.addZero(day)} ${transFormation.addZero(hour)}:${transFormation.addZero(mins)}:${transFormation.addZero(seconds)}`;
return time;
},
// 獲取當前時間前一天
getPrevDate() {
const now = new Date();
const date = new Date(now - 1000 * 60 * 60 * 24);
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
const hour = date.getHours();
const mins = date.getMinutes();
const seconds = date.getSeconds();
const prevTime = `${year}-${transFormation.addZero(month)}-${transFormation.addZero(day)} ${transFormation.addZero(hour)}:${transFormation.addZero(mins)}:${transFormation.addZero(seconds)}`;
return prevTime;
},
// 當前時間的前兩個小時
getPrevHour() {
const now = new Date();
const date = new Date(now - 2 * 60 * 60 * 1000);
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
const hour = date.getHours();
const mins = date.getMinutes();
const seconds = date.getSeconds();
const prevHour = `${year}-${transFormation.addZero(month)}-${transFormation.addZero(day)} ${transFormation.addZero(hour)}:${transFormation.addZero(mins)}:${transFormation.addZero(seconds)}`;
return prevHour;
},
// 如果日期時間是單數(shù)前面添加0
addZero(s) {
return s < 10 ? `0${s}` : s;
},
// 標準時間轉(zhuǎn)換成日期時間格式
timestampToTime(timestamp) {
const d = new Date(timestamp);
const resDate = `${d.getFullYear()}-${transFormation.addZero((d.getMonth() + 1))}-${transFormation.addZero(d.getDate())}`;
const resTime = `${transFormation.addZero(d.getHours())}:${transFormation.addZero(d.getMinutes())}:${transFormation.addZero(d.getSeconds())}`;
const formDate = `${resDate} ${resTime}`;
return formDate;
},
// 將圖片url轉(zhuǎn)成base64編碼格式
// getBase64Image(img) {
// const canvas = document.createElement('canvas'); // 創(chuàng)建一個canvas
// canvas.width = img.width; // 設(shè)置對應(yīng)的寬高
// canvas.height = img.height;
// const ctx = canvas.getContext('2d'); // 二維繪圖環(huán)境
// ctx.drawImage(img, 0, 0, img.width, img.height); // 將圖片畫在畫布上
// const ext = img.src.substring(img.src.lastIndexOf('.') + 1).toLowerCase(); // 獲取到圖片的格式
// const dataURL = canvas.toDataURL(`image/${ext}`); // 得到base64 編碼的 dataURL
// return dataURL;
// },
};
export default transFormation;
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。