Add/Subtract months/years to date in dart?

后端 未结 7 2079
感动是毒
感动是毒 2021-01-01 08:57

I saw that in dart there is a class Duration but it cant be used add/subtract years or month. How did you managed this issue, I need to subtract 6 months from an date. Is th

相关标签:
7条回答
  • 2021-01-01 09:26

    Okay so you can do that in two steps, taken from @zoechi (a big contributor to Flutter):

    Define the base time, let us say:

    var date = new DateTime(2018, 1, 13);
    

    Now, you want the new date:

    var newDate = new DateTime(date.year, date.month - 1, date.day);
    

    And you will get

    2017-12-13
    
    0 讨论(0)
  • 2021-01-01 09:28

    Not so simple.

    final date = DateTime(2017, 1, 1);
    final today = date.add(const Duration(days: 1451));
    

    This results in 2020-12-21 23:00:00.000 because Dart considers daylight to calculate dates (so my 1451 days is missing 1 hour, and this is VERY dangerous (for example: Brazil abolished daylight savings in 2019, but if the app was written before that, the result will be forever wrong, same goes if the daylight savings is reintroduced in the future)).

    To ignore the dayligh calculations, do this:

    final date = DateTime(2017, 1, 1);
    final today = DateTime(date.year, date.month, date.day + 1451);
    

    Yep. Day is 1451 and this is OK. The today variable now shows the correct date and time: 2020-12-12 00:00:00.000.

    0 讨论(0)
  • 2021-01-01 09:35

    You can use the subtract and add methods

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

    Flutter Docs:

    Subtract

    Add

    0 讨论(0)
  • 2021-01-01 09:40

    Use the add and subtract methods with a Duration object to create a new DateTime object based on another.

    var date1 = DateTime.parse("1995-07-20 20:18:04");
    
    var newDate = date1.add(Duration(days: 366));
    
    print(newDate); // => 1996-07-20 20:18:04.000
    

    Notice that the duration being added is actually 50 * 24 * 60 * 60 seconds. If the resulting DateTime has a different daylight saving offset than this, then the result won't have the same time-of-day as this, and may not even hit the calendar date 50 days later.

    Be careful when working with dates in local time.

    0 讨论(0)
  • 2021-01-01 09:49

    Can subtract any count of months.

      DateTime subtractMonths(int count) {
        var y = count ~/ 12;
        var m = count - y * 12;
    
        if (m > month) {
          y += 1;
          m = month - m;
        }
    
        return DateTime(year - y, month - m, day);
      }
    

    Also works

    DateTime(date.year, date.month + (-120), date.day);
    
    0 讨论(0)
  • 2021-01-01 09:50

    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); // 2018-07-13 00:00:00.000
    

    You can also do chaining using 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
    
    0 讨论(0)
提交回复
热议问题