How to format DateTime in Flutter

前端 未结 9 1760
悲哀的现实
悲哀的现实 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

    Use this function

    todayDate() {
        var now = new DateTime.now();
        var formatter = new DateFormat('dd-MM-yyyy');
        String formattedTime = DateFormat('kk:mm:a').format(now);
        String formattedDate = formatter.format(now);
        print(formattedTime);
        print(formattedDate);
      }
    

    Output:

    08:41:AM
    21-12-2019
    

提交回复
热议问题