JS 时间比较,判断截止时间

/**
 * 截至日期格式示例:2022-06-02 15:00:00
 * true:到了截止时间,false:未到截止时间
 */
export function getDeadline(str) {
  const myDate = new Date()
  const year = myDate.getFullYear()
  const mon = (myDate.getMonth() + 1).toString().padStart(2, '0')
  const date = myDate.getDate().toString().padStart(2, '0')
  const hour = myDate.getHours().toString().padStart(2, '0')
  const minutes = myDate.getMinutes().toString().padStart(2, '0')
  const seconds = myDate.getSeconds().toString().padStart(2, '0')
  const now = `${year}-${mon}-${date} ${hour}:${minutes}:${seconds}`
  const newDate = now.replace(/-/g, '/')
  const time = new Date(newDate).getTime()
  const sureTime = new Date(str.replace(/-/g, '/')).getTime()
  return time >= sureTime
}
posted @ 2023-02-27 19:32  jiazq  阅读(107)  评论(0)    收藏  举报