你好,我有这个格式的日期示例:2019 年 6 月 4 日
DateTime now = DateTime.now();
formattedDate = DateFormat.yMMMMd("fr_FR").format(now);
我现在尝试在按下按钮后增加或减少天数,但之前的代码不兼容:
var newDate= now.add(new Duration(days: changedate));
最佳答案
首先:定义你的格式化程序:
final _dateFormatter = DateFormat.yMMMMd("fr_FR");
然后你需要有一个方法来接收你的日期和天数(正数会增加天数,负数会减少):
DateTime changeDate(DateTime dtObj, int numberOfDays) {
return dtObj.add(Duration(days: numberOfDays));
}
然后,当你需要显示你的日期时,你可以格式化它:
String formattedDate = _dateFormatter.format(previouslyChangedDate);
print(formattedDate); //prints as: 4 juin 2019
关于flutter - 如何增加或减少日期格式输出的天数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56443374/