Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with Minute Precision in FormatDateRange for Entire Period (Months, Quarters, Years) Ranges #10

Open
abdurezakfarah opened this issue Aug 29, 2024 · 1 comment

Comments

@abdurezakfarah
Copy link

The formatDateRange function currently uses minute precision to determine if a date range spans a full year. However, an issue arises when the end date is set to the start of the day (00:00:00) or other minutes, rather than 59 on the final date of the range. To accurately identify a full-year range, the function requires the end time to be 23:59:59. To improve accuracy, it's necessary to ignore the minute precision check for year-long ranges.

Example

When formatting a range from:

Start Date: Mon Jan 01 2024 00:00:00

End Date: Tue Dec 31 2024 00:00:00

The function incorrectly formats this as a month range "Jan 1 - Dec 31" instead of "2024". This is because the function expects the end time to be exactly 23:59:59.

Suggested Fix

Update the code to skip minute-level checks for year and month ranges. Use broader checks like isSameDay instead.

Current Code:

 // Check if the range is the entire year
 // Example: 2023
  if (
    isSameMinute(startOfYear(from), from) &&
    isSameMinute(endOfYear(to), to)
  ) {
    return ${format(from, "yyyy")};
  }

Updated Code:

 // Check if the range is the entire year
 // Example: 2023
if (
  isSameDay(startOfYear(from), from) && 
  isSameDay(endOfYear(to), to)
) {
  return `${format(from, "yyyy")}`;
}

In addition to the year-long ranges, similar adjustments are needed for other periods such as months and quarters. The same approach can be applied to handle these periods more accurately.

@abdurezakfarah
Copy link
Author

Apologies for the accidental closure! I’ve reopened the issue as it still needs to be addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant