spring
11 小时以前 4b8f0d1cb618b00303502681159b0ad6bc4404a6
src/utils/ruoyi.js
@@ -224,3 +224,53 @@
      return true;
    }
}
/**
* 将日期转换为YYYY-MM-DD格式
* @param {*} dateSource 日期源(可以是时间戳或Date对象)
* @returns 格式化后的日期字符串 YYYY-MM-DD
*/
export function formatDateToYMD(dateSource) {
   let date;
   // 处理:如果是时间戳,先转为Date对象
   if (typeof dateSource === 'number') {
      const timestamp = dateSource.toString().length === 10 ? dateSource * 1000 : dateSource;
      date = new Date(timestamp);
   }
   // 处理:如果是Date对象,直接使用
   else if (dateSource instanceof Date) {
      date = dateSource;
   }
   // 处理:如果是字符串,做兼容转换
   else if (typeof dateSource === 'string') {
      const raw = dateSource.trim();
      if (!raw) return '';
      if (/^[0-9]+$/.test(raw)) {
         const n = parseInt(raw, 10);
         const timestamp = raw.length === 10 ? n * 1000 : n;
         date = new Date(timestamp);
      } else {
         // iOS / 部分 WebView 对 YYYY-MM-DD 兼容较差,统一转 /
         const normalized = raw
            .replace(new RegExp(/-/gm), '/')
            .replace('T', ' ')
            .replace(new RegExp(/\.[\d]{3}/gm), '');
         date = new Date(normalized);
      }
   }
   // 异常情况:返回空
   else {
      return '';
   }
   if (!(date instanceof Date) || Number.isNaN(date.getTime())) return '';
   // 补零函数:确保月/日是两位数
   const padZero = (num) => num.toString().padStart(2, '0');
   const year = date.getFullYear();
   const month = padZero(date.getMonth() + 1);
   const day = padZero(date.getDate());
   return `${year}-${month}-${day}`;
}