How do I implement the current time into Text format? I feel like it should be fairly simple but struggling to do so.

Basic example;

enter image description here


Solution 1: Peter Haddad

Using the answer here and changing it a bit:-

You can try the following:

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

void main() {
 runApp(TabBarDemo());
}

class TabBarDemo extends StatelessWidget {
 @override
Widget build(BuildContext context) {
DateTime now = DateTime.now();
String formattedDate = DateFormat('kk:mm:ss \n EEE d MMM').format(now);
return MaterialApp(
  home: DefaultTabController(
    length: 3,
    child: Scaffold(
      appBar: AppBar(
        bottom: TabBar(
          tabs: [
            Tab(icon: Icon(Icons.access_alarm),text: "Alarm",),
            Tab(icon: Icon(Icons.access_time),text:"Clock" ,),
            Tab(icon: Icon(Icons.timer),text:"Timer"),
          ],
        ),
        title: Text('Tabs Demo'),backgroundColor: Colors.black,
      ),
      body: TabBarView(
        children: [
          Icon(Icons.access_alarm),
          Center(child: Text(formattedDate,textAlign: TextAlign.center,style: new TextStyle(fontWeight: FontWeight.bold,fontSize: 25.0),)),
          Icon(Icons.timer),
        ],
      ),
    ),
  ),
);
}
}

Should give you this:

flutter image


Solution 2: RohanArihant

To get the current Time in AM/PM format.

dynamic currentTime = DateFormat.jm().format(DateTime.now());


Solution 3: Mohsen Emami

In flutter 1.20 :

DateTime now = DateTime.now();

print(now.hour.toString() + ":" + now.minute.toString() + ":" + now.second.toString());


Solution 4: Iago G. Nunes

var now = DateTime.now();
var formatterDate = DateFormat('dd/MM/yy');
var formatterTime = DateFormat('kk:mm');
String actualDate = formatterDate.format(now);
String actualTime = formatterTime.format(now);


Solution 5: BusterT

To set a continuous running clock:

void initState() {
_timeString = _formatTime(DateTime.now());

Timer.periodic(const Duration(seconds: 1), (Timer t) => _getTimeString());
super.initState();

}

Then use to get time:

_getTimeString() {
final DateTime now = DateTime.now();
final String formattedTime = _formatTime(now);

setState(() {
  _timeString = formattedTime;    
});

}

Use to format time:

String _formatTime(DateTime dateTime) {
return DateFormat('hh:mm:ss').format(dateTime);

}

To display the time, create a text widget and use

'$_timeString'

to display continuously updating clock.