/** * @description 日期转换工具类 */ /** * 2020-07-12T11:39:35.000+00:00 =》 2020-07-12 12:24:18 * 如果是当天的话 只显示时分秒 * @returns {string} * @param date */ export function toDayTime(date) { if (!date) return ""; // Convert input date to Date object const inputDate = new Date(date); // Get current date const now = new Date(); // Check if the input date is today const isToday = inputDate.getDate() === now.getDate() && inputDate.getMonth() === now.getMonth() && inputDate.getFullYear() === now.getFullYear(); // Adjust for timezone offset (+8 hours) const adjustedDate = new Date(+inputDate + 8 * 3600 * 1000); if (isToday) { // Return only time (HH:MM:SS) return adjustedDate .toISOString() .split("T")[1] .replace(/\.[\d]{3}Z/, ""); } else { // Return full date and time (YYYY-MM-DD HH:MM:SS) return adjustedDate .toISOString() .replace(/T/g, " ") .replace(/\.[\d]{3}Z/, ""); } } /** * 2020-07-12T11:39:35.000+00:00 =》 2020-07-12 12:24:18 * @returns {string} * @param date */ export function renderTime(date) { if (!date) return ""; const dateee = new Date(date).toJSON(); return new Date(+new Date(dateee) + 8 * 3600 * 1000) .toISOString() .replace(/T/g, " ") .replace(/\.[\d]{3}Z/, ""); } /** * // 时间戳转日期(包含年月日时分秒) * @param timestamp * @returns {string} */ export function timestampToTime(timestamp) { if (timestamp) { let date = new Date(parseInt(timestamp)); let Y = date.getFullYear() + "-"; let M = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "-"; let D = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(); let h = (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":"; let m = (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) + ":"; let s = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds(); return Y + M + D + " " + h + m + s; } else { return ""; } } export function renderDate(date) { if (!date) return ""; const fmt = "yyyy-MM-dd"; return renderDefault(date, fmt); } export function renderDefault(data, fmt) { if (data == null) { return null; } const date = new Date(data); const o = { "M+": date.getMonth() + 1, //月份 "d+": date.getDate(), //日 "h+": date.getHours(), //小时 "m+": date.getMinutes(), //分 "s+": date.getSeconds(), //秒 "q+": Math.floor((date.getMonth() + 3) / 3), //季度 S: date.getMilliseconds(), //毫秒 }; if (/(y+)/.test(fmt)) { fmt = fmt.replace( RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length) ); } for (const k in o) { if (new RegExp("(" + k + ")").test(fmt)) { fmt = fmt.replace( RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length) ); } } return fmt; } /** * 日期字符串转年月日 * @param time * @returns {string} */ export function timeToYMD(time) { if (time) { let date = new Date(time); let Y = date.getFullYear() + "-"; let M = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "-"; let D = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(); return Y + M + D; } else { return ""; } } // let datetransform = { // // 将日期转换成含T的格式不含秒 // toTformat(times) { // var time = times // if (time) { // var date = new Date(time) // let Y = date.getFullYear() + '-' // let M = // (date.getMonth() + 1 < 10 // ? '0' + (date.getMonth() + 1) // : date.getMonth() + 1) + '-' // let D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate() // let h = // (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':' // let m = // date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes() // let s = // date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds() // return Y + M + D + 'T' + h + m // } else { // return '' // } // }, // // 将日期转换成标准格式 // tonormalformat(times) { // var time = times // if (time) { // var date = new Date(time) // let Y = date.getFullYear() + '-' // let M = // (date.getMonth() + 1 < 10 // ? '0' + (date.getMonth() + 1) // : date.getMonth() + 1) + '-' // let D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate() // let h = // (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':' // let m = // (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + // ':' // let s = // date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds() // return Y + M + D + ' ' + h + m + s // } else { // return '' // } // }, // // 日期转时间戳 // tostamp(times) { // if (times) { // let time = new Date(times) // return time.getTime() // } else { // return '' // } // }, // // 日期转时间戳(零点) // tozerostamp(times) { // if (times) { // let time = new Date(times + ' 00:00:00') // return time.getTime() // } else { // return '' // } // }, // // // 时间戳转日期(包含年月日) // getday(timestamp) { // if (timestamp) { // let date = new Date(parseInt(timestamp)) // let Y = date.getFullYear() + '-' // let M = // (date.getMonth() + 1 < 10 // ? '0' + (date.getMonth() + 1) // : date.getMonth() + 1) + '-' // let D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate() // let h = // (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':' // let m = // (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + // ':' // let s = // date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds() // return Y + M + D // } else { // return '' // } // }, // // 时间戳转日期(包含时分秒) // gethour(timestamp) { // if (timestamp) { // let date = new Date(parseInt(timestamp)) // let Y = date.getFullYear() + '-' // let M = // (date.getMonth() + 1 < 10 // ? '0' + (date.getMonth() + 1) // : date.getMonth() + 1) + '-' // let D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate() // let h = // (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':' // let m = // (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + // ':' // let s = // date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds() // return h + m + s // } else { // return '' // } // }, // // 日期/时分秒,参数为空时返回当前日期时间 type为"day"时返回当天日期 type为"hour"时返回当天时间 type空时返回当天日期加时间 // getnow(type) { // let date = new Date() // let Y = date.getFullYear() + '-' // let M = // (date.getMonth() + 1 < 10 // ? '0' + (date.getMonth() + 1) // : date.getMonth() + 1) + '-' // let D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate() // let h = // (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':' // let m = // (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + // ':' // let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds() // if (type == 'day') { // return Y + M + D // } else if (type == 'hour') { // return h + m + s // } else { // return Y + M + D + ' ' + h + m + s // } // }, // }