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

getDay method does not transition to "Yesterday" after midnight, requiring a full 24-hour difference #281

Open
limwlee opened this issue Dec 4, 2024 · 0 comments · May be fixed by #287

Comments

@limwlee
Copy link

limwlee commented Dec 4, 2024

Describe the bug
Currently, the getDay method on the extension.dart requires a full 24-hour difference to display "yesterday."

For example:

  • On May 23rd at 11:59 PM, I send a message, and it correctly shows "Today."
  • However, after 12:00 AM on May 24th, the message still shows "today" instead of "Yesterday," which is confusing because the message was sent on the previous calendar day.

This behavior leads to a misleading user experience where a message sent "yesterday" continues to display as "today."

To Reproduce
Steps to reproduce the behavior:

  1. Send a message on any day (e.g., May 23rd) close to midnight (e.g., 11:59 PM).
  2. Check the message display after midnight (e.g., on May 24th at 12:01 AM).
  3. Observe that the message still shows "today" instead of "yesterday."

Expected behavior
Messages sent on the previous calendar day (before midnight) should display "yesterday" after midnight, regardless of whether 24 hours have passed.

Screenshots
image
Below message in the image was send on Dec 02, but now is Dec 03 is still shows "today" instead of "yesterday."

Additional context
This issue might occur because the current logic uses difference(DateTime.now()).inDays, which is based on the full 24-hour difference. For a more natural user experience, the method could compare only the calendar date (ignoring time).

Suggested correction:

extension TimeDifference on DateTime {
  String getDay(String chatSeparatorDatePattern) {
    final now = DateTime.now();

    final targetDate = DateTime(year, month, day);
    final currentDate = DateTime(now.year, now.month, now.day);

    final differenceInDays = currentDate.difference(targetDate).inDays;

    if (differenceInDays == 0) {
      return PackageStrings.today;
    } else if (differenceInDays == 1) {
      return PackageStrings.yesterday; 
    } else {
      final DateFormat formatter = DateFormat(chatSeparatorDatePattern);
      return formatter.format(this); 
    }
  }

  String get getDateFromDateTime {
    final DateFormat formatter = DateFormat(dateFormat);
    return formatter.format(this);
  }

  String get getTimeFromDateTime => DateFormat.Hm().format(this);
}

This modification ensures that the getDay method considers only the calendar day transition (e.g., after midnight), rather than requiring a full 24-hour difference.

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