How I can show only current time using DateTime.now() in flutter?

Issue

 DateTime currentTime = new DateTime.now();

  Widget CustomCards(Color frontColor, String fetchTime) {
    Card cards = new Card(
      elevation: 1.0,
      margin: const EdgeInsets.symmetric(horizontal: 2.0, vertical: 2.0),
      color: bgColor,
      shape: new RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(15.0),
      ),
      child: Column(
        children: [
          Padding(
            padding: const EdgeInsets.all(5.0),
            child: Row(
              children: [
                Padding(
                  padding: const EdgeInsets.only(left: 15.0),
                  child: Text(
                  currentTime.toString(),
                  style: new TextStyle(
                    fontStyle: FontStyle.italic,
                    fontFamily: 'Satisfy',
                    fontSize: 15.0,
                    color: frontColor,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
    return cards;
  }

How can I show the current time from DateTime, as it always shows the current date along with time but I want to show the time in format hh:mm:ss. Here seconds should be necessarily their. I am not able to find any logic please help me.
Thank You

Solution

Add Dependency in pubspec.yaml intl: ^0.16.1 (check latest dependency from pub.dev) then you can use DateFormat.yMMMd().format(DateTime.now()); in your dart file. You can change Pattern of Date format based on your requirement.

For custom date pattern which does not exists in DateFormat set then you can use

DateFormat('hh:mm:ss').format(DateTime.now());

Answered By – Nik

Answer Checked By – Dawn Plyler (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *