How can I get the current date (w/o hour and minutes)?

前端 未结 7 1595
攒了一身酷
攒了一身酷 2020-12-05 12:33

All I can see in the documentation is Datetime.now() but it returns the Timespan, I need just the date.

7条回答
  •  南笙
    南笙 (楼主)
    2020-12-05 13:34

    If you want only the date without the timestamp. You can take the help of intl package.

    main() {
        var now = new DateTime.now();
        var formatter = new DateFormat('yyyy-MM-dd');
        String formattedDate = formatter.format(now);
        print(formattedDate); // 2016-01-25
    } 
    

    This requires the intl package:

    dependencies:
      intl: ^0.16.1
    

    And finally import:

    import 'package:intl/intl.dart';
    

提交回复
热议问题