Flutter/Dart Extract just the Date and Time? [duplicate]
Since the provided timestamp is supported by DateTime.parse
you could do something like this:
void main() { final dateTime = DateTime.parse('2021-08-11T11:38:09.000Z'); print(convertDateTimeToString(dateTime)); // 2021-08-11 11:38}String convertDateTimeToString(DateTime dt) => '${dt.year}-' '${dt.month.toString().padLeft(2, '0')}-' '${dt.day.toString().padLeft(2, '0')} ' '${dt.hour.toString().padLeft(2, '0')}:' '${dt.minute.toString().padLeft(2, '0')}';
Alternative, you can use a package like intl
which supports formatting DateTime
using a formatting pattern:
import 'package:intl/intl.dart';void main() { final dateTime = DateTime.parse('2021-08-11T11:38:09.000Z'); final format = DateFormat('yyyy-MM-dd HH:mm'); print(format.format(dateTime)); // 2021-08-11 11:38}
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:.