0

I would like to either display the date if a message was not sent today, or the time if the message was sent today.

I use the Firebase server on which I save the server TimeStamp for every message I send.

This is an excerpt from my code:

 Text(
    snapshot.data.documents[0]["time"] == DateTime.now()
        ? DateFormat.Hm().format(snapshot.data.documents[0]["time"].toDate()).toString()
        : DateFormat.yMd().format(snapshot.data.documents[0]["time"].toDate()).toString(),
    style: TextStyle(
      color: Colors.grey,
      fontSize: 15.0,
      fontWeight: FontWeight.bold,
    ),
  )

Unfortunately, my code only gives me the date. In addition, the date is output as follows: 3/27/2021. However, I want the date to be displayed as follows: 03/27/2021.

Christopher Moore
  • 15,626
  • 10
  • 42
  • 52
Вадим
  • 93
  • 1
  • 10

2 Answers2

1

This is my final code. It works perfectly now. Thanks again for your help! :)

                                      Text(
                                      DateTime.now()
                                                  .difference(snapshot
                                                      .data.documents[0]["time"]
                                                      .toDate())
                                                  .inHours ==
                                              0
                                          ? DateFormat.Hm()
                                              .format(snapshot
                                                  .data.documents[0]["time"]
                                                  .toDate())
                                              .toString()
                                          : DateFormat('dd.MM.yyyy')
                                              .format(snapshot
                                                  .data.documents[0]["time"]
                                                  .toDate())
                                              .toString(),
                                    ),
Вадим
  • 93
  • 1
  • 10
0

1st case would be like

DateTime dateTime = DateTime.now();

Text(dateTime.difference(yourCurrentDay).inDays == 0 ? DateFormat.Hm()
                                          .format(snapshot
                                              .data.documents[0]["time"]
                                              .toDate())
                                          .toString()
                                      : DateFormat.yMd()
                                      .format(snapshot
                                      .data.documents[0]["time"]
                                      .toDate())
                                      .toString())

Try the simple format method. Your question at the end was not clear.

 DateFormat formater = DateFormat('MM/dd/yyyy');
 String formatted = formater.format(now);
 print(formatted); 

 03/27/2021

more here.... enter link description here

EngineSense
  • 3,266
  • 8
  • 28
  • 44