Add/Subtract months/years to date in dart?

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

提交回复
热议问题