{

How to Add/subtract years, months, days, hours, minutes, or seconds to a DateTime Dart or Flutter


 Add/subtract years, months, days, hours, minutes, or seconds to a DateTime  in Dart

In this tutorial, You will find multiple ways to add or subtract years, months, days, hours, minutes, or seconds.

  • Add Date years, months, days, and time to DateTime add(Duration), Duration can be days, hours, minutes, or seconds.
  • Subtract Date years, months, days and time to DateTime subtract(Duration), Duration can be days, hours, minutes, or seconds.

How to add year months days, hours minutes or seconds milliseconds, and microseconds to date in Dart

  • First Create a Date object for the Current Date and time
  • Date class contains add the method that accepts the Duration object
DateTime add(Duration duration)

Duration can be passed with days, hours, minutes, seconds, mill, and microseconds.

Here is a program to add duration to the date

void main() {
  DateTime now = DateTime.now(); // March 2022

  var date = new DateTime(now.year, now.month + 1, 0);

  print(date); //2022-04-30 00:00:00.000

  //add one Year
  date = date.add(Duration(days: 365));
  print(date); //2023-04-30 00:00:00.000

  //add one Month
  date = date.add(Duration(days: 30));
  print(date); // 2023-05-30 00:00:00.000

  //add one day
  date = date.add(Duration(days: 1));
  print(date); // 2023-05-31 00:00:00.000

  //add one hour
  date = date.add(Duration(hours: 1));
  print(date); // 2023-05-31 01:00:00.000

  //add 30 minutes
  date = date.add(Duration(minutes: 30));
  print(date); //2023-05-31 01:30:00.000

  //add 30 seconds
  date = date.add(Duration(seconds: 30));
  print(date); //2023-05-31 01:30:30.000
  //add 40 milliseconds
  date = date.add(Duration(milliseconds: 40));
  print(date); //2023-05-31 01:30:30.040

  //add 1000 microseconds
  date = date.add(Duration(microseconds: 1000));
  print(date); //2023-05-31 01:30:30.041
}

Output:

2022-04-30 00:00:00.000
2023-04-30 00:00:00.000
2023-05-30 00:00:00.000
2023-05-31 00:00:00.000
2023-05-31 01:00:00.000
2023-05-31 01:30:00.000
2023-05-31 01:30:30.000
2023-05-31 01:30:30.040
2023-05-31 01:30:30.041

How to Subtract years months days, hours minutes or seconds milliseconds, and microseconds to date in Dart

  • First Create a Date object for the Current Date and time
  • Date class contains subtract method that accepts Duration object
DateTime subtract(Duration duration)

Duration can be passed with days, hours, minutes, seconds, mill, and microseconds. Here is a program to subtract duration to date

void main() {
  DateTime now = DateTime.now();

  var date = new DateTime(now.year, now.month + 1, 0);

  print(date); //2022-04-30 00:00:00.000

  //subtract one Year
  date = date.subtract(Duration(days: 365));
  print(date); //2021-04-30 00:00:00.000

  //subtract one Month
  date = date.subtract(Duration(days: 30));
  print(date); // 2021-03-31 00:00:00.000

  //subtract one day
  date = date.subtract(Duration(days: 1));
  print(date); // 2021-03-30 00:00:00.000


  //subtract one hour
  date = date.subtract(Duration(hours: 1));
  print(date); // 2021-03-29 23:00:00.000

  //add 30 minutes
  date = date.subtract(Duration(minutes: 30));
  print(date); //2021-03-29 22:30:00.000

  //subtract 30 seconds
  date = date.subtract(Duration(seconds: 30));
  print(date); //2021-03-29 22:29:30.000
  
  //subtract 40 milliseconds
  date = date.subtract(Duration(milliseconds: 40));
  print(date); //2021-03-29 22:29:29.960

  //subtract 1000 microseconds
  date = date.subtract(Duration(microseconds: 1000));
  print(date); //2021-03-29 22:29:29.959
}

Output:

2022-04-30 00:00:00.000
2021-04-30 00:00:00.000
2021-03-31 00:00:00.000
2021-03-30 00:00:00.000
2021-03-29 23:00:00.000
2021-03-29 22:30:00.000
2021-03-29 22:29:30.000
2021-03-29 22:29:29.960
2021-03-29 22:29:29.959
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

Perl How to: Find a Given Variable String is numeric or not

Dart/Flutter: Check if String is Empty, Null, or Blank example

How to generate Unique Id UUID in Dart or Flutter Programming| Dart or Flutterby Example

How to Sort List of numbers or String in ascending and descending in Dart or Flutter example

Dart Enum comparison operator| Enum compareByIndex example| Flutter By Example

Dart Example - Program to check Leap year or not

Dart Example to count the number of digits in a number| Decimals Count in Dart