Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter/Dart Extract just the Date and Time? [duplicate]

How would I either reformat, convert or extract just the date and time from this string in Dart/Flutter? The String comes from the default timestamp in my MySQL database;

2021-08-11T11:38:09.000Z

So what I need is something like this;

  2021-08-11 11:38

I was thinking perhaps something like;

final String? shortime = timestamp?.replaceAll(RegExp(''), '') ?? ''; 

But I'm not sure what to use as the regex.

like image 723
Meggy Avatar asked Sep 11 '25 17:09

Meggy


1 Answers

you can use this code:

DateTime x = DateTime.parse("2021-08-11T11:38:09.000Z");

then create a function to check the input number:

String checkNum(int x){
   return x<10?"0${x}":x.toString();
}

now you can use this function as follow:

  print(checkNum(x.year));
  print(checkNum(x.month)); // and so on...

let me know if you had any related problems. here is an example: enter image description here.

like image 186
Abbasihsn Avatar answered Sep 14 '25 09:09

Abbasihsn