简体   繁体   中英

How to remove last 3 numbers from this line? Flutter

Hello there i have this Datacells and i want to remove the last 3 numbers, here is my code line:

DataCell(Text((DateTime.parse(e['start'].toString()).toLocal().toString()))),

在此处输入图像描述

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.

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;
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(Text(truncatedDateString));

Simply take the first 20 characters

DateTime.parse(e['start'].toString()).toLocal().toString().substring(0,19); //2022-06-01 14:28:56

or as mmcdon20 said use the intl package

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM