Reputation: 1811
I would like to know nice ways to get today's 0am as a DateTime
variable. Could you advise me?
Examples
// Suppose today is April 18, 2020
DateTime _dateTime = {DO_SOMETHING};
print(_dateTime); // Expected output: 2020-04-18 00:00:00
// Suppose today is October 22, 2019
DateTime _dateTime = {DO_SOMETHING};
print(_dateTime); // Expected output: 2019-10-22 00:00:00
Upvotes: 1
Views: 70
Reputation: 107171
As far as I know, there is no built-in methods available to get only date. You can construct it in the following way:
DateTime dateTime = DateTime.now();
DateTime date = DateTime(dateTime.year, dateTime.month, dateTime.day);
print(date); // Prints 2020-04-18 00:00:00.000
You can also use intl or jiffy packages to convert the dates in your desired formats.
Upvotes: 1