[英]How to remove last 3 numbers from this line? Flutter
Try the following code:试试下面的代码:
DataCell(Text((DateTime.parse(e['start'].toString()).toLocal().toString()).replaceAll(".333", ""))),
You can take this as an example:你可以以此为例:
final text = "2022-06-12 16:34:45:333";
print(text.substring(0, text.length-4)); // 2022-06-12 16:34:45
This will remove the last 3 letters and ":" from it.这将从中删除最后 3 个字母和“:”。
Another solution, you can also do this:另一个解决方案,你也可以这样做:
final text = "2022-06-12 16:34:45:333";
final asList = text.split(":");
asList.removeLast();
print(asList.join(":")); // 2022-06-12 16:34:45
you can use the substring method on the string you get;你可以在你得到的字符串上使用substring方法;
In particular, you can first calculate how the string should look like:特别是,您可以先计算字符串的外观:
String dateString = DateTime.parse(e['start'].toString()).toLocal().toString();
String truncatedDateString = dateString.substring(0, dateString.length - 4);
and then use the truncatedDateString variable in your DataCell:然后在您的 DataCell 中使用 truncatedDateString 变量:
DataCell(Text(truncatedDateString));
Simply take the first 20 characters只需取前 20 个字符
DateTime.parse(e['start'].toString()).toLocal().toString().substring(0,19); //2022-06-01 14:28:56
or as mmcdon20 said use the intl package或者如mmcdon20所说,使用intl 包
import 'package:intl/intl.dart';
...
DateTime now = DateTime.now();
String formattedDate = DateFormat('yyyy-MM-dd – kk:mm:ss').format(now); //2022-12-19 – 10:32:05
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.