Add/Subtract months/years to date in dart?

后端 未结 7 2194
感动是毒
感动是毒 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
    

提交回复
热议问题