第一种:
// 日期处理函数 export const timeHandle = (para) => { const tempDate = new Date(para.replace("CST", "GMT 0800")), oMonthT = (tempDate.getMonth() 1).toString(), oMonth = oMonthT.length <= 1 ? "0" oMonthT : oMonthT, oDayT = tempDate.getDate().toString(), oDay = oDayT.length <= 1 ? "0" oDayT : oDayT, oYear = tempDate.getFullYear().toString(), oHourT = tempDate.getHours().toString(), oHour = oHourT.length <= 1 ? "0" oHourT : oHourT, oMinuteT = tempDate.getMinutes().toString(), oMinute = oMinuteT.length <= 1 ? "0" oMinuteT : oMinuteT, oTime = oYear '-' oMonth '-' oDay " " oHour ":" oMinute; return oTime; };
第二种:可以处理时间戳格式
// 时间日期处理 export function dateHandle02 (para) { const add0 = (m) => { return m < 10 ? '0' m : m; }; ///时间戳是整数,否则parseInt转换 const time = new Date(para), y = time.getFullYear(), m = time.getMonth() 1, d = time.getDate(), h = time.getHours(), mm = time.getMinutes(), s = time.getSeconds(); // return y '-' add0(m) '-' add0(d) ' ' add0(h) ':' add0(mm) ':' add0(s); const t = { t1: y '-' add0(m) '-' add0(d) ' ' add0(h) ':' add0(mm) ':' add0(s), t2: y '-' add0(m) '-' add0(d) } return t; };
第三种:
// 时间格式处理 function formatNumber (n) { const str = n.toString(); return str[1] ? str : `0${str}`; } export function formatTime (date) { const year = date.getFullYear(); const month = date.getMonth() 1; const day = date.getDate(); const hour = date.getHours(); const minute = date.getMinutes(); const second = date.getSeconds(); const t1 = [year, month, day].map(formatNumber).join('); const t2 = [hour, minute, second].map(formatNumber).join(」; return `${t1} ${t2}`; }