How to Add/Subtract Months/Years to Date in Dart?

· 2 min read
How to AddSubtract MonthsYears to Date in Dart
How to AddSubtract MonthsYears to Date in Dart

In this series of DateTime Picker earlier we have been through multiple articles like Date&Time Picker Widget, Date Range Picker Widget So now in this article we will go through How to Add/Subtract Months/Years to Date in Dart?

How to Add/Subtract Months/Years to Date in Dart?

User can use the Subtract and Add Methods like the below:

date1.subtract(Duration(days: 7, hours: 3, minutes: 43, seconds: 56));
date1.add(Duration(days: 1, hours: 23)));

Try out this package, Jiffy. Adds and subtracts date time with respect to how many days there are in a month and also leap years. It follows the simple syntax of momentjs.

You can add and subtract using the following units.

years, months, weeks, days, hours, minutes, seconds and milliseconds

To add 6 Months.

DateTime d = Jiffy().add(months: 6); // 2020-04-26 10:05:57.469367
// You can also add you own Datetime object
DateTime d = Jiffy(DateTime(2018, 1, 13)).add(months: 6);

You can also do chaining using the dart method cascading.

var jiffy = Jiffy()
  ..add(months: 5, years: 1);

DateTime d = jiffy.dateTime; // 2021-03-26 10:07:10.316874
// you can also format with ease
String s = jiffy.format("yyyy, MMM"); // 2021, Mar
// or default formats
String s = jiffy.yMMMMEEEEdjm; // Friday, March 26, 2021 10:08 AM

Conclusion:

In this article, We have been through How to Add/Subtract Months/Years to Date in Dart?

Thanks for Reading !!!
Do let us know your feedback/comments on the same.

FlutterAgency.com is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

FlutterAgency.com is one of the most popular online portal dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge on Flutter.

Leave a Reply