Skip to content

getDateDiffDays ​

获取两个日期之间的天数间隔

引入版本 ​

0.110.0

Demo ​

ts
import { getDateDiffDays } from '@wyfex/iutils'

console.log('=== 默认自然日模式 ===')
console.log(getDateDiffDays('2026-09-01 23:59:59', '2026-09-02 00:00:01')) // 1
console.log(getDateDiffDays('2026-09-01', '2026-09-04')) // 3
console.log(getDateDiffDays('2026-09-04', '2026-09-01')) // 3
console.log(getDateDiffDays('2026-09-01', '2026-09-01')) // 0

console.log('\n=== 时间戳模式 isTimestampMode:true ===')
console.log(
  getDateDiffDays('2026-09-01 23:59:59', '2026-09-02 00:00:01', {
    isTimestampMode: true
  })
) // 0
console.log(
  getDateDiffDays('2026-09-01 00:00:00', '2026-09-03 00:00:00', {
    isTimestampMode: true
  })
) //2

console.log('\n=== UTC自然日模式 ===')
// UTC时间 2026-09-01 23点 和 09-02 01点,UTC日期跨天,差值1
console.log(
  getDateDiffDays('2026-09-01T23:00:00Z', '2026-09-02T01:00:00Z', {
    useUTC: true
  })
) //1

基于 MIT 许可发布