How to Get 1 Day Before Today in Flutter/Dart

In this example, you will learn to get one day before today, or you can say yesterday. You will also learn to get any number of days or times before the current time in Dart or Flutter. Here, we will subtract time from the current time.

See this first: How to Add or Subtract Days from DateTime in Dart/Flutter

DateTime today = DateTime.now();
print(today); //2023-01-16 09:51:27.494057

DateTime yesterday = today.subtract(Duration(days: 1));
print(yesterday); //2023-01-15 09:51:27.494057

Here, we got the yesterday by subtracting one day from the current date and time. You can also subtract hours, minutes, or seconds like below:

DateTime today = DateTime.now();
print(today); //2023-01-16 09:51:27.494057

DateTime yes1 = today.subtract(Duration(days: 1, hours: 4, minutes: 30));
print(yes1); //2023-01-15 05:21:27.494057

DateTime today = DateTime.now();
print(today); //2023-01-16 09:51:27.494057

DateTime weeksago = today.subtract(Duration(days: 7));
print(weeksago); //2023-01-09 09:51:27.494057

Here, we have subtracted 7 days from the current date and time and got the date and time a week ago. You can also, convert any DateTime string to DateTime and subtract like above: How to Convert String to DateTime in Dart/Flutter

In this way, you can get 1 day or 1 week before the current date time in Flutter or Dart.

No any Comments on this Article


Please Wait...