Date format: dynamic string depending on how long ago

Date format: dynamic string depending on how long ago

You probably have seen applications that format dates in a verbose way:

  • “Just now” when it’s been less than a minute
  • Only the time (xx:xx) if it was today and longer ago than a minute
  • “Yesterday, xx:xx” if it was yesterday
  • The weekday and the time if it was within the last couple of days
  • The exact date and time if it was longer ago

This type of format can be found in popular chat apps for example in the chat overview. Let’s implement a date formatter that puts out a string in the above mentioned way.

We start by implementing a new class called DateFormatter with a single public method getVerboseDateTimeRepresentation. We let it expect a UTC DateTime as its purpose is to receive a DateTime and return a string.

1class DateFormatter {
2  String getVerboseDateTimeRepresentation(DateTime dateTime) {
3  }

Just now

The first case we deal with is returning “Just now” if the given DateTime is less than a minute old.

1DateTime now =;
2DateTime justNow = 1));
3DateTime localDateTime = dateTime.toLocal();
5if (!localDateTime.difference(justNow).isNegative) {
6  return 'Just now';

It’s important to make the comparison with the local DateTime as it ensures that it works across every timezone. Otherwise, the result of the difference would always be affected by the difference of the local timezone to the UTC timezone.


Next step is to show the rough time (meaning that it omits the seconds) whenever the given DateTime is less than a day old.

1String roughTimeString = DateFormat('jm').format(dateTime);
2if ( == && localDateTime.month == now.month && localDateTime.year == now.year) {
3  return roughTimeString;

You might wonder why 'jm' is used as the positional newPattern argument for the DateTime constructor. That’s because it adapts to the circumstances of the current locale. If we were to use DateFormat('HH:mm'), we would always have 24 hour time format whereas DateFormat('jm') would use 12 hour time and add am / pm markers if needed. For more information about the difference of skeletons and explicit patterns have a look at the docs.

We could also use the ICU name instead of the skeleton. In this case DateFormat('WEEKDAY') would work as well and is certainly better readable.


Now we want the DateFormatter to prepend Yesterday, if the given DateTime holds a value that represents the day before today.

1DateTime yesterday = now.subtract(Duration(days: 1));
3if ( == && localDateTime.month == yesterday.month && localDateTime.year == yesterday.year) {
4  return 'Yesterday, ' + roughTimeString;

We check whether day, month and year of the current DateTime subtracted by a day equal the respective values of the given DateTime and return Yesterday, followed by the rough time string we stored above if the condition evaluates to true.

Last couple of days

Let’s deal with everything less old than 4 days and return the weekday in the system’s language followed by the hours and minutes.

1if (now.difference(localDateTime).inDays < 4) {
2  String weekday = DateFormat('EEEE').format(localDateTime);
4  return '$weekday, $roughTimeString';

We compare the current DateTime with the given DateTime and check whether the difference is less than 4 whole days. If so, we use the skeleton EEEE that represents the verbose weekday. Because we don’t provide the second optional argument, it takes en_US as the locale and returns the weekday in that language.

Again, we could also use the ICU name instead of the skeleton so DateFormat('<mark>HOUR_MINUTE</mark>') would work as well.

Otherwise, return year, month and day

Now if none of the above conditions match, we want to display the date and the time.

1return '${DateFormat('yMd').format(dateTime)}, $roughTimeString';

So now we have achieved what we wanted to: depending on how long ago the given DateTime was, we want to return different strings.

Localization of DateFormatter

One thing that this formatter is still lacking is localization. If we used this on a device whose system language is not English, we would still be faced with English expressions.
In order to fix that, we need the current system’s locale. That’s not enough, though, as we also want the phrases "Just now" and "Yesterday" to be translated. That’s why we need localization in general and take the locale from the delegate. Have a look at the i18n tutorial for information on how to set that up.


2  "dateFormatter_just_now": "Just now",
3  "dateFormatter_yesterday": "Yesterday"


2  "dateFormatter_just_now": "Gerade eben",
3  "dateFormatter_yesterday": "Gestern"
 1import 'package:flutterclutter/app_localizations.dart';
 2import 'package:intl/intl.dart';
 4class DateFormatter {
 5  DateFormatter(this.localizations);
 7  AppLocalizations localizations;
 9  String getVerboseDateTimeRepresentation(DateTime dateTime) {
10    DateTime now =;
11    DateTime justNow = now.subtract(Duration(minutes: 1));
12    DateTime localDateTime = dateTime.toLocal();
14    if (!localDateTime.difference(justNow).isNegative) {
15      return localizations.translate('dateFormatter_just_now');
16    }
18    String roughTimeString = DateFormat('jm').format(dateTime);
20    if ( == && localDateTime.month == now.month && localDateTime.year == now.year) {
21      return roughTimeString;
22    }
24    DateTime yesterday = now.subtract(Duration(days: 1));
26    if ( == && localDateTime.month == now.month && localDateTime.year == now.year) {
27      return localizations.translate('dateFormatter_yesterday');
28    }
30    if (now.difference(localDateTime).inDays < 4) {
31      String weekday = DateFormat('EEEE', localizations.locale.toLanguageTag()).format(localDateTime);
33      return '$weekday, $roughTimeString';
34    }
36    return '${DateFormat('yMd', localizations.locale.toLanguageTag()).format(dateTime)}, $roughTimeString';
37  }

We add AppLocalization as the only argument to the constructor of the DateFormatter. Every occasion of a string that contains a language-specific phrase is now altered by the usage of AppLocalization or its locale property.

Now, in order to see our brand new formatter in action, we create a widget that lists hardcoded chats and displays the date of the last sent message in the top right corner.

 1import 'package:flutter/material.dart';
 2import 'date_formatter.dart';
 3import 'app_localizations.dart';
 5class Chat {
 6  Chat({
 7    @required this.sender,
 8    @required this.text,
 9    @required this.lastMessageSentAt
10  });
12  String sender;
13  String text;
14  DateTime lastMessageSentAt;
17class MessageBubble extends StatelessWidget{
18  MessageBubble({
19    this.message
20  });
22  final Chat message;
24  @override
25  Widget build(BuildContext context) {
26    return Padding(
27      child: Column(
28        crossAxisAlignment: CrossAxisAlignment.start,
29        children: <Widget>[
30          Row(
31            mainAxisAlignment: MainAxisAlignment.end,
32            children: [
33              Text(
34                DateFormatter(AppLocalizations.of(context)).getVerboseDateTimeRepresentation(message.lastMessageSentAt),
35                textAlign: TextAlign.end,
36                style: TextStyle(
37                  color: Colors.grey
38                )
39              )
40            ]
41          ),
42          Padding(
43            padding: EdgeInsets.only(bottom: 16),
44            child: Text(
45              message.sender,
46              style: TextStyle(
47                fontSize: 18,
48                fontWeight: FontWeight.bold
49              )
50            ),
51          ),
52          Text(
53            message.text,
54          ),
55        ],
56      ),
57      padding: EdgeInsets.all(16)
58    );
59  }
62class DateTimeList extends StatelessWidget {
63  final List<Chat> messages = [
64    Chat(
65      sender: 'Sam',
66      text: 'Sorry man, I was busy!',
67      lastMessageSentAt: 27))
68    ),
69    Chat(
70      sender: 'Neil',
71      text: 'Hey! Are you there?',
72      lastMessageSentAt: 3))
73    ),
74    Chat(
75      sender: 'Patrick',
76      text: 'Hey man, what\'s up?',
77      lastMessageSentAt: 7))
78    ),
79  ];
81  @override
82  Widget build(BuildContext context) {
83    return ListView.separated(
84      itemBuilder: (BuildContext context, int index) {
85        return MessageBubble(message: messages[index]);
86      },
87      separatorBuilder: (BuildContext context, int index) => Divider(),
88      itemCount: messages.length
89    );
90  }


Here is a comparison between the above implemented list with:

  • Chats with dates being displayed without our DateFormatter
  • Chats with dates being displayed with our DateFormatter and English locale
  • Chats with dates being displayed with our DateFormatter and German locale
Absolute date formatting
Date format without using our DateFormater
Verbose date formatting in English
Date format using our DateFormatter
Verbose date formatting in German
Date format using our Dateformatter with locale de


We have implemented a class that returns a verbose representation of the DateTime object that is provided and adaptively changes depending on how far the date reaches into the past. We have also made this class handle different locales using localization.
This formatter can be useful in several contexts where a date is supposed to be displayed relative to the current date.

Comments (2) ✍️


You may consider to refactor those conditions to have unified structure, eg.:

now.difference(localDateTime).inDays &lt; 4 - I found this one to be most readable, then first condition may be changed from:

!localDateTime.difference(justNow).isNegative to: now.difference(localDateTime).inMinutes &lt;= 1

same principle applies to yesterday and rough date cases.

Reply to zgorawski

In reply to zgorawski's comment

That is much more readable! Good suggestion, will do that! :)
Reply to Marc

Comment this 🤌

You are replying to 's commentRemove reference