Flutter:如何在String中用mounth更改dateTime格式? [英] Flutter : How to change format dateTime with mounth in String?
问题描述
你好我没有成功以这种格式输出此代码(2019年6月4日)
hello I don't succeded to output this code in this format ( 04 Juin 2019 )
var now = new DateTime.now();
var daysfromnow = now.add(new Duration(days: changedate));
RegExp regExp = new RegExp(
r"(^\S*)",
);
var match = regExp.firstMatch("$daysfromnow");
daysfromnow_modify = match.group(1);
当前outpub:2019-06-04
current outpub : 2019-06-04
预期产量:2019年6月4日
expected output : 04 juin 2019
更新:
我尝试格式化,但是按按钮时我没有成功更改日期...
I tried to format but I don't succeded to change days when I press button...
这是新代码
new IconButton(
icon: Icon(Icons.keyboard_arrow_left),
onPressed: () {
setState(() {
changedate--;
DateTime now = DateTime.now();
formattedDate = DateFormat.yMMMMd("en_FR").format(now);
daysfromnow = formattedDate.add(new Duration(days: changedate)); //add can't be a String ...
});
)}
我也尝试过..
formattedDate = DateFormat.yMMMMd("en_FR").now.add(new Duration(days: changedate));
推荐答案
您可以尝试使用DateFormat,只需包含 intl 对pubspec.yaml的依赖
You can try to use a DateFormat, just include intl dependency to your pubspec.yaml
import 'package:intl/intl.dart';
DateTime now = DateTime.now();
String formattedDate = DateFormat("dMMMMy")format(now);
print(formattedDate);
根据您的要求,您可以查看 DateFormat
Depending on what your requirements is, you can look at DateFormat
从DateFormat类获取的一些示例可以为您提供更多帮助.
Some examples taken from DateFormat-class to help you a bit more.
String formattedDate = DateFormat.yMd(); // 7/10/1996
String formattedDate = DateFormat("yMd"); // 7/10/1996
String formattedDate = DateFormat.yMMMMd("en_US"); // July 10, 1996
String formattedDate = DateFormat.yMMMMd("en_FR"); // 10 July 1996 // Locale French
更新
首先将值添加到日期,然后调用格式.
First add the value to the date and then call the format.
示例:
setState(() {
changedate--;
var now = DateTime.now(); // Current Date
var daysFromNow = now.add(new Duration(days: changedate); // Assuming this is the date you want (Variable is unclear)
String formattedDate = DateFormat.yMMMMd('en_FR').format(daysFromNow);
print(formattedDate);
// daysfromnow = formattedDate // This cannot exist due to the formattedDate is a `String`
});
这篇关于Flutter:如何在String中用mounth更改dateTime格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!