title: Converting DateTime to time ago in Dart/Flutter
body: <p>The question is how to format a Dart DateTime as a string stating the time elapsed similar to the way you see times displayed on Stack Overflow.</p>
  
  <p>Is there any better method than this</p>
  
  <pre><code>String timeAgo(DateTime d) {
   Duration diff = DateTime.now().difference(d);
   if (diff.inDays &gt; 365)
    return "${(diff.inDays / 365).floor()} ${(diff.inDays / 365).floor() == 1 ? "year" : "years"} ago";
   if (diff.inDays &gt; 30)
    return "${(diff.inDays / 30).floor()} ${(diff.inDays / 30).floor() == 1 ? "month" : "months"} ago";
   if (diff.inDays &gt; 7)
    return "${(diff.inDays / 7).floor()} ${(diff.inDays / 7).floor() == 1 ? "week" : "weeks"} ago";
   if (diff.inDays &gt; 0)
    return "${diff.inDays} ${diff.inDays == 1 ? "day" : "days"} ago";
   if (diff.inHours &gt; 0)
    return "${diff.inHours} ${diff.inHours == 1 ? "hour" : "hours"} ago";
   if (diff.inMinutes &gt; 0)
    return "${diff.inMinutes} ${diff.inMinutes == 1 ? "minute" : "minutes"} ago";
   return "just now";
  }
  </code></pre>
  
  <p>Thank you and hope it helps others</p>