export function dateFormat(date, fmt) { let ret; const opt = { "Y+": date.getFullYear().toString(), // 年 "m+": (date.getMonth() + 1).toString(), // 月 "d+": date.getDate().toString(), // 日 "H+": date.getHours().toString(), // 时 "M+": date.getMinutes().toString(), // 分 "S+": date.getSeconds().toString(), // 秒 // 有其他格式化字符需求可以继续添加,必须转化成字符串 }; for (let k in opt) { ret = new RegExp("(" + k + ")").exec(fmt); if (ret) { fmt = fmt.replace(ret[1], ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, "0")); } } return fmt; } export function formatDateToYYYYMMDD(dateString) { // 创建一个 Date 对象 const date = new Date(dateString); // 获取年、月、日部分 const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, "0"); // 月份从0开始,所以要加1,并且用padStart补0 const day = String(date.getDate()).padStart(2, "0"); // 用padStart补0 // 拼接成 YYYY.MM.DD 格式 return `${year}.${month}.${day}`; } export function formatDate(dateStr) { // 创建一个 Date 对象 const date = new Date(dateStr); // 提取年、月、日、小时、分钟和秒 const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, "0"); // 月份从0开始,需要加1 const day = String(date.getDate()).padStart(2, "0"); const hours = String(date.getHours()).padStart(2, "0"); const minutes = String(date.getMinutes()).padStart(2, "0"); const seconds = String(date.getSeconds()).padStart(2, "0"); // 格式化日期字符串 const formattedDate = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`; return formattedDate; }