Skip to content

Instantly share code, notes, and snippets.

@srph
Created November 15, 2022 05:05
Show Gist options
  • Save srph/3bee677a5f5ee8e0608bc6efd14cd023 to your computer and use it in GitHub Desktop.
Save srph/3bee677a5f5ee8e0608bc6efd14cd023 to your computer and use it in GitHub Desktop.
date-fns: Convert duration months to days
import { intervalToDuration, isAfter, eachDayOfInterval, addMonths } from 'date-fns'
const remaining = intervalToDuration({
start: now,
end: target
})
const days =
remaining.months && remaining.days
? remaining.days +
eachDayOfInterval({
start: now,
end: addMonths(now, remaining.months)
}).length
: remaining.days
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment