Skip to content

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

Description

@a95z

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions