import dayjs, { Dayjs } from 'dayjs';

const DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm';
const DATE_FORMAT = 'YYYY-MM-DD ';

export function formatToDateTime(date: Date | Dayjs | string, formatStr = DATE_TIME_FORMAT): string {
  return dayjs(date).format(formatStr);
}

export function formatToDate(date: Date | Dayjs | string, formatStr = DATE_FORMAT): string {
  return dayjs(date).format(formatStr);
}

/**
 * 获取当前时间的年月日时分格式字符串
 * 格式：YYYYMMDDHHmm (例如：202509051538)
 * @returns 格式化后的时间字符串
 */
export const getCurrentDateTimeString = (): string => {
  const now = new Date();
  const year = now.getFullYear().toString();
  // 月、日、时、分需要补零，确保是两位数
  const month = String(now.getMonth() + 1).padStart(2, '0');
  const day = String(now.getDate()).padStart(2, '0');
  const hours = String(now.getHours()).padStart(2, '0');
  const minutes = String(now.getMinutes()).padStart(2, '0');

  // 拼接成指定格式
  return year + month + day + hours + minutes;
};
