Get Last Month Date In Flutter / Dart

前端 未结 6 1170
故里飘歌
故里飘歌 2021-01-11 18:37

in flutter we can get current month using this

var now = new DateTime.now();
var formatter = new DateFormat(\'MM\');
String month = formatter.format(now);
<         


        
相关标签:
6条回答
  • 2021-01-11 18:48

    We can calculate both first day of the month and the last day of the month:

    DateTime firstDayCurrentMonth = DateTime.utc(DateTime.now().year, DateTime.now().month, 1);

    DateTime lastDayCurrentMonth = DateTime.utc(DateTime.now().year,DateTime.now().month+1,).subtract(Duration(days: 1));

    DateTime.utc takes in integer values as parameters: int year, int month, int day and so on.

    0 讨论(0)
  • 2021-01-11 18:49

    We can use the subtract method to get past month date.

    DateTime pastMonth = DateTime.now().subtract(Duration(days: 30));
    
    0 讨论(0)
  • 2021-01-11 18:54

    You can just use

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

    with

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

    you get

    2017-12-13
    

    It's usually a good idea to convert to UTC and then back to local date/time before doing date calculations to avoid issues with daylight saving and time zones.

    0 讨论(0)
  • 2021-01-11 18:54

    Dates are pretty hard to calculate. There is an open proposal to add support for adding years and months here https://github.com/dart-lang/sdk/issues/27245.

    There is a semantic problem with adding months and years in that "a month" and "a year" isn't a specific amount of time. Years vary by one day, months by up to three days. Adding "one month" to the 30th of January is ambiguous. We can do it, we just have to pick some arbitrary day between the 27th of February and the 2nd of March. That's why we haven't added month and year to Duration - they do not describe durations.

    You can use the below code to add months in a arbitrary fashion (I presume its not completely accurate. Taken from the issue)

    const _daysInMonth = const [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    
    bool isLeapYear(int value) =>
        value % 400 == 0 || (value % 4 == 0 && value % 100 != 0);
    
    int daysInMonth(int year, int month) {
      var result = _daysInMonth[month];
      if (month == 2 && isLeapYear(year)) result++;
      return result;
    }
    
    DateTime addMonths(DateTime dt, int value) {
      var r = value % 12;
      var q = (value - r) ~/ 12;
      var newYear = dt.year + q;
      var newMonth = dt.month + r;
      if (newMonth > 12) {
        newYear++;
        newMonth -= 12;
      }
      var newDay = min(dt.day, daysInMonth(newYear, newMonth));
      if (dt.isUtc) {
        return new DateTime.utc(
            newYear,
            newMonth,
            newDay,
            dt.hour,
            dt.minute,
            dt.second,
            dt.millisecond,
            dt.microsecond);
      } else {
        return new DateTime(
            newYear,
            newMonth,
            newDay,
            dt.hour,
            dt.minute,
            dt.second,
            dt.millisecond,
            dt.microsecond);
      }
    }
    
    0 讨论(0)
  • 2021-01-11 19:07

    In addition to Günter Zöchbauer Answer

    var now = new DateTime.now();
        String g = ('${now.year}/ ${now.month}/ ${now.day}');
        print(g);
    
    0 讨论(0)
  • 2021-01-11 19:14

    Try this package, Jiffy, it used momentjs syntax. See below

    Jiffy().subtract(months: 1);
    

    Where Jiffy() returns date now. You can also do the following, the same result

    var now = DateTime.now();
    Jiffy(now).subtract(months: 1);
    

    Jiffy respects the leap years and how many days there are in each month

    0 讨论(0)
提交回复
热议问题