I have a very simple question. I am working on time and date now and I have an issue.
Text('Time is: ${date.hour}:${date.minute}')
When I write something like this, where date is a DateTime object, when minute is less than 10, it is displayed like
14:5
I want to display it in 14:05 format, in other words, in determined 2 digit length, like %.2d in C. How can I do it?
CodePudding user response:
You can do it with padLeft method.
${date.hour.toString().padLeft(2, "0")}:${date.minute.toString().padLeft(2, "0")}
CodePudding user response:
In flutter, we can use the DateFormat method for this purpose.
import 'package:intl/intl.dart';
DateTime date = DateTime.now();
String formattedDate = DateFormat('HH:mm').format(date);
Text('Time is: $formattedDate')