zibidigonzales
zibidigonzales

Reputation: 597

How to Display Number in Determined Length in Flutter

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?

Upvotes: 0

Views: 473

Answers (2)

DEFL
DEFL

Reputation: 1052

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')

Upvotes: 1

Teroaz
Teroaz

Reputation: 300

You can do it with padLeft method.

${date.hour.toString().padLeft(2, "0")}:${date.minute.toString().padLeft(2, "0")}

Upvotes: 3

Related Questions