How to format DateTime in Flutter

前端 未结 9 1775
悲哀的现实
悲哀的现实 2020-12-04 10:28

I am trying to display the current DateTime in a Text widget after tapping on a button. The following works, but I\'d like to change the format.

9条回答
  •  没有蜡笔的小新
    2020-12-04 11:07

    With this approach, there is no need to import any library.

    DateTime now = DateTime.now();
    
    String convertedDateTime = "${now.year.toString()}-${now.month.toString().padLeft(2,'0')}-${now.day.toString().padLeft(2,'0')} ${now.hour.toString()}-${now.minute.toString()}";
    

提交回复
热议问题