How to format TimeOfDay to String in flutter

12,070

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

Share:
12,070
ahmed minhaj
Author by

ahmed minhaj

Updated on June 09, 2022

Comments

  • ahmed minhaj
    ahmed minhaj almost 2 years

    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.