/** * 时间戳转时间字符串 * @param timestamp * @returns {string} */ export function timestampToTime(timestamp) { if (!timestamp) return ''; timestamp = timestamp.toString().length === 10 ? timestamp * 1000 : timestamp; let date = new Date(timestamp); //时间戳为10位需*1000,时间戳为13位的话不需乘1000 let Y = date.getFullYear() + '-'; let M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-'; let D = date.getDate() + ' '; let h = date.getHours() + ':'; let m = date.getMinutes() + ':'; let s = date.getSeconds(); return Y+M+D+h+m+s; } /** * 时间字符串转时间戳 * @param timeStr * @param isSecond * @returns {number} */ export function timeStrToTimeStamp(timeStr,isSecond){ let date = new Date(timeStr); if(isSecond){ return date.getTime()/1000; }else{ return date.getTime(); } } /** * 时间戳转时间字符串 * @param time 时间戳 * @param format 格式 yyyy-MM-dd HH:mm:ss * @returns {*} 格式化后的时间字符串 */ export function timeFormat(time, format) { let t = new Date(time); let tf = function(i) { return (i < 10 ? '0' : '') + i }; return format .replace(/yyyy/,tf(t.getFullYear())) .replace(/MM/,tf(t.getMonth() + 1)) .replace(/M/,String(t.getMonth() + 1)) .replace(/dd/,tf(t.getDate())) .replace(/d/,String(t.getDate())) .replace(/HH/,tf(t.getHours())) .replace(/H/,String(t.getHours())) .replace(/mm/,tf(t.getMinutes())) .replace(/m/,String(t.getMinutes())) .replace(/ss/,tf(t.getSeconds())) .replace(/s/,String(t.getSeconds())); } export function getUnixTimeStamp(){ return Math.round(new Date().getTime()/1000); }