云
2026-09-29 682124f3e573a24523dd643f23d16bb7d3c7e08d
src/utils/index.js
@@ -12,14 +12,58 @@
      ? "0" + (date.getMonth() + 1)
      : date.getMonth() + 1;
  var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
  var minutes =
    date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
  var seconds =
    date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
  return (
    year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds
  );
  return year + "-" + month + "-" + day;
}
/**
 * 仅保留日期部分,去掉时分秒,输出 YYYY-MM-DD。
 * 兼容 Date 对象、"YYYY-MM-DD HH:mm:ss"、"YYYY-MM-DDTHH:mm:ss" 等格式;
 * 已是纯日期或非日期字符串时原样返回。
 */
export function dateOnly(value) {
  if (value == null || value === "") return "";
  if (value instanceof Date) {
    if (Number.isNaN(value.getTime())) return "";
    const pad = (num) => String(num).padStart(2, "0");
    return `${value.getFullYear()}-${pad(value.getMonth() + 1)}-${pad(
      value.getDate()
    )}`;
  }
  const str = String(value).trim();
  return str.split(/[ T]/)[0] || str;
}
/**
 * el-table-column 日期列格式化:仅保留日期部分,去掉时分秒。
 */
export function formatDateColumn(row, column, cellValue) {
  return dateOnly(cellValue);
}
/**
 * 补全时分秒,输出 YYYY-MM-DD HH:mm:ss,用于后端 LocalDateTime 字段。
 * 兼容 Date 对象、"YYYY-MM-DD"、"YYYY-MM-DD HH:mm"、"YYYY-MM-DDTHH:mm:ss" 等输入;
 * 空值原样返回。
 */
export function toDateTimeString(value) {
  if (value == null || value === "") return value;
  if (value instanceof Date) {
    if (Number.isNaN(value.getTime())) return "";
    const pad = (num) => String(num).padStart(2, "0");
    return `${value.getFullYear()}-${pad(value.getMonth() + 1)}-${pad(
      value.getDate()
    )} ${pad(value.getHours())}:${pad(value.getMinutes())}:${pad(
      value.getSeconds()
    )}`;
  }
  const [datePart, timePart = ""] = String(value)
    .trim()
    .replace("T", " ")
    .split(/\s+/);
  const timeUnits = timePart.split(":").filter((unit) => unit !== "");
  while (timeUnits.length < 3) timeUnits.push("00");
  const time = timeUnits.map((unit) => unit.padStart(2, "0")).join(":");
  return `${datePart} ${time}`;
}
/**