How can I get the current date (excluding hours and minutes) in Darth?

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

Thanks.

+4
source share
3 answers

Create a new date from nowwith only the parts you need:

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

The main libraries do not have a class for modeling dates without time. You must use new DateTime.now().

Remember that the date depends on the time zone: 2016-01-20 02:00:00in Paris - this is the same moment as 2016-01-19 17:00:00in Seattle, but the day is not the same.

+2
source

If you want only a date without a timestamp. You can use the 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 an intl package.

0
source

Source: https://habr.com/ru/post/1625210/


All Articles