export const formatDateTime = (dateTime: string | Date | null | undefined): string => {
  if (!dateTime) return '-'

  try {
    const date = typeof dateTime === 'string' ? new Date(dateTime) : dateTime
    
    const year = date.getFullYear()
    const month = String(date.getMonth() + 1).padStart(2, '0')
    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')

    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
  } catch (error) {
    console.error('时间格式化失败:', error)
    return String(dateTime)
  }
}

export const formatDate = (date: string | Date | null | undefined): string => {
  if (!date) return '-'

  try {
    const d = typeof date === 'string' ? new Date(date) : date
    
    const year = d.getFullYear()
    const month = String(d.getMonth() + 1).padStart(2, '0')
    const day = String(d.getDate()).padStart(2, '0')

    return `${year}-${month}-${day}`
  } catch (error) {
    console.error('日期格式化失败:', error)
    return String(date)
  }
}

export const formatTime = (time: string | Date | null | undefined): string => {
  if (!time) return '-'

  try {
    const t = typeof time === 'string' ? new Date(time) : time
    
    const hours = String(t.getHours()).padStart(2, '0')
    const minutes = String(t.getMinutes()).padStart(2, '0')
    const seconds = String(t.getSeconds()).padStart(2, '0')

    return `${hours}:${minutes}:${seconds}`
  } catch (error) {
    console.error('时间格式化失败:', error)
    return String(time)
  }
}
