I want to display the current time. Used TimeOfDay.Now() to get the current time,
TimeOfDay _currentTime = TimeOfDay.now();
Text("Current Time: ${_currentTime.toString()}")
Used Text() widget to display time, but it displays TimeOfDay(22.30) instead of 10.30pm.
How to remove TimeOfDay from TimeOfDay(22.30), want to display only 10.30pm.
Convert it to 24 hours format If we set alwaysUse24HourFormat to true, it might convert to 24 hours format but we need to clone the default setting and then change the value in somewhere.
You can create TimeOfDay using the constructor which requires both hour and minute or using DateTime object. Hours are specified between 0 and 23, as in a 24-hour clock. See also: showTimePicker, which returns this type.
To format DateTime in Flutter using a standard format, you need to use the intl library and then use the named constructors from the DateFormat class. Simply write the named constructor and then call the format() method with providing the DateTime.
Doing a toString
to a TimeOfDay
object will return you the default implementation of toString
which as per documentation:
Returns a string representation of this object.
What are you looking for here is to format
it instead.
TimeOfDay _currentTime = TimeOfDay.now();
Text("Current Time: ${_currentTime.format(context)}")
Please also take a look at the official documentation in order to have a better understanding of why toString
is not doing what you expect.
https://api.flutter.dev/flutter/material/TimeOfDay-class.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With