Calculate the difference in days between two times

The input parameter is in the format of new Date('2023-01-13 10:14:30')

export function getDiffDay(date_1, date_2) {
  if (!date_1 || !date_2) return 0
  const myDate_1 = Date.parse(date_1)
  const myDate_2 = Date.parse(date_2)
  const diffDate = Math.abs(myDate_1 - myDate_2)
  const totalDays = Math.floor(diffDate / (1000 * 3600 * 24))
  return totalDays
}

The Date.parse() function is used to analyze a string containing a date and return the number of milliseconds between the date and midnight on January 1, 1970. This function belongs to the Date object and is supported by all major browsers.

 Reference: js - get the number of days or time between two dates (days + hours + minutes + seconds) - basic accumulation_js get the number of days between two dates_Ye Haocheng 520's Blog-CSDN Blog

Guess you like

Origin blog.csdn.net/qq_37548296/article/details/130225600