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

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


Solution 1:

Create a new date from now with only the parts you need:

DateTime now = new DateTime.now();
DateTime date = new DateTime(now.year, now.month, now.day);

Solution 2:

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';