flutterdatedartsubtraction

Add/Subtract months/years to date in dart?


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 there something like moment.js for dart or something around? Thank you


Solution

  • 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 = DateTime(2018, 1, 13);
    

    Now, you want the new date:

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

    And you will get

    2017-12-13