Skip to content

best way to do isAfter with variable precision? #2916

Answered by srevenant
srevenant asked this question in Q&A
Discussion options

You must be logged in to vote

I'm playing with this right now and it seems to be doing the job. I'm doing more testing...

export function isSameOrAfter(target, type, origin = new Date()) {
  switch (type) {
    case 'year':
      return startOfYear(target) >= startOfYear(origin)
    case 'month':
      return startOfMonth(target) >= startOfMonth(origin)
    case 'day':
      return startOfDay(target) >= startOfDay(origin)
    case 'hour':
      return startOfHour(target) >= startOfHour(origin)
    case 'minute':
      return startOfMinute(target) >= startOfMinute(origin)
    default:
      throw new Error(`isSameOrAfter - unrecognized precision type: ${type}!`)
  }
}

Replies: 3 comments 3 replies

Comment options

You must be logged in to vote
3 replies
@srevenant
Comment options

@fturmel
Comment options

@leshakoss
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by srevenant
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants