Get Last Month Date In Flutter / Dart

前端 未结 6 1172
故里飘歌
故里飘歌 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: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.

提交回复
热议问题