{

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


In this tutorial, You will find multiple ways to add or subtract years, months, 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 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 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 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

Dart| Flutter How to: Find a Given String is numeric or not

How to iterate loop Enum in Dart | Get Enum with index in Flutter By Example

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

Dart tutorial examples/ Flutter By Examples

How to add Methods or Values to Enums in Dart | Flutter By Example

How to convert Double to Integer or Integer to double in Dart| Flutter By Example

How to: Enum to String in Dart | Print Enum name as String in Flutter By Example