skip to Main Content

I want to get time difference like –

‘2h 28mins ago’, ‘a few seconds ago’, ‘a few mins ago’,’1 min ago’, ‘6h ago’,’a day ago’,’a week ago’, ‘a few months ago’

I get event occured time as DateTime object. I can get current time with DateTime.now() I want to calculate the difference between this 2 times. I’m currently doing this using many lines of if else statements like this…

if (now.year > date.year) {
      //months ago
      if ((12 - date.month) + now.month > 11) {
        return "a year ago";
      } else if ((12 - date.month) + now.month > 1) {
        return "${(12 - date.month) + now.month} months ago";
      } else if ((12 - date.month) + now.month == 1) {
        if ((getDateCountForMonth(date.month) - date.day) + now.day > 13) {
          return "a few weeks ago";
        }
...}}

Is there a better and simple way to do this? I want to output like this – '2h 28mins ago', 'a few seconds ago', 'a few mins ago','1 min ago', '6h ago','a day ago','a week ago', 'a few months ago'

3

Answers


  1. Lets say the selected date is

    DateTime date = DateTime(2022, 9, 7, 17, 30);
    

    you can calculate the difference by calling difference on DateTime format like this:

    date.difference(DateTime.now());
    

    and you can get this difference in many format like in days, inHours, in inMinutes ,… :

    var days = date.difference(DateTime.now()).inDays;
    

    and with this the calculation may get easier.

    if (days > 365) {
     //years ago
    } else if (days < 365 && days > 31)  {
     // month ago
    } else {
     ...
    }
    
    Login or Signup to reply.
  2. I created a function to calculate the time difference for a project in the past, you can make use of this function. Just call it by passing the DateTime string which you want to compare with the current timing:

    String getComparedTime(String dateTime) {
        Duration difference = DateTime.now().difference(DateTime.parse(dateTime));
        final List prefix = [
          "just now",
          "second(s) ago",
          "minute(s) ago",
          "hour(s) ago",
          "day(s) ago",
          "month(s) ago",
          "year(s) ago"
        ];
        if (difference.inDays == 0) {
          if (difference.inMinutes == 0) {
            if (difference.inSeconds < 20) {
              return (prefix[0]);
            } else {
              return ("${difference.inSeconds} ${prefix[1]}");
            }
          } else {
            if (difference.inMinutes > 59) {
              return ("${(difference.inMinutes / 60).floor()} ${prefix[3]}");
            } else {
              return ("${difference.inMinutes} ${prefix[2]}");
            }
          }
        } else {
          if (difference.inDays > 30) {
            if (((difference.inDays) / 30).floor() > 12) {
              return ("${((difference.inDays / 30) / 12).floor()} ${prefix[6]}");
            } else {
              return ("${(difference.inDays / 30).floor()} ${prefix[5]}");
            }
          } else {
            return ("${difference.inDays} ${prefix[4]}");
          }
        }
      }
    
    Login or Signup to reply.
  3. String timeAgo({bigDate, smallDate, bool numericDates = false}) {
      final difference = bigDate.difference(smallDate);
      if ((difference.inDays / 7).floor() >= 1) {
        return (numericDates) ? '1 week ago' : 'Last week';
      } else if (difference.inDays >= 2) {
        return '${difference.inDays} days ago';
      } else if (difference.inDays >= 1) {
        return (numericDates) ? '1 day ago' : 'Yesterday';
      } else if (difference.inHours >= 2) {
        return '${difference.inHours} hours ago';
      } else if (difference.inHours >= 1) {
        return (numericDates) ? '1 hour ago' : 'An hour ago';
      } else if (difference.inMinutes >= 2) {
        return '${difference.inMinutes} minutes ago';
      } else if (difference.inMinutes >= 1) {
        return (numericDates) ? '1 minute ago' : 'A minute ago';
      } else if (difference.inSeconds >= 3) {
        return '${difference.inSeconds} seconds ago';
      } else {
        return 'Just now';
      }
    }
    

    /// and use like

    String date = timeAgo(smallDate: DateTime(2022, 12, 14, 10, 00), bigDate: DateTime(2022, 12, 14, 12, 00),);
    print('difference between to dates: $date');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search