问题
I would like to know how I can change the datetime format in flutter, to leave it only with the date. I also want to know if there is another widget that only contains the date.
Thanks
回答1:
You can use DateFormat from intl package. With it you can custom to any format that you need.
Add to pubspec.yaml
dependencies:
intl: ^0.15.7
On Dart code:
import 'package:intl/intl.dart';
final dateFormatter = DateFormat('yyyy-MM-dd');
final dateString = dateFormatter.format(DateTime.now());
Then you can put the string into any widget. Example:
Text(dateString)
About date format: https://www.cl.cam.ac.uk/~mgk25/iso-time.html
回答2:
Make a new DateTime and get the day from that:
var date = DateTime.now();
var result = "${date.year}/${date.month}/${date.day}";
print(result); // prints 2019/3/6
回答3:
I dont know if its the right way but it gets the job done easily.
DateTime today = DateTime.now();
print(today); // prints 2019-12-15 00:00:00.000
DatetTime date = DateTime(today.year, today.month, today.day).toString().substring(0,10);
print(date); // prints 2019-12-15
hope this helps
来源:https://stackoverflow.com/questions/55023657/change-the-datetime-format-in-flutter-to-leave-it-only-with-the-date