change the datetime format in flutter, to leave it only with the date

非 Y 不嫁゛ 提交于 2021-02-07 10:15:41

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!