32

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

Tom O'Sullivan
  • 3,416
  • 5
  • 15
  • 27

6 Answers6

55

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

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • Thanks! Do you know how to make the time stay up to date? Would it need an animation class? – Tom O'Sullivan Aug 05 '18 at 22:39
  • 2
    No, just a timer that calls `setState`. There's an interesting article [here](https://medium.freecodecamp.org/how-fast-is-flutter-i-built-a-stopwatch-app-to-find-out-9956fa0e40bd). – Richard Heap Aug 05 '18 at 23:33
  • 1
    Note that DateFormat is relatively expensive to construct (for one thing, it has to parse the format string), so it would be better not to rebuild that each time. – Alan Knight Aug 06 '18 at 18:03
  • how to date countdown from now until chosen date? – phuocding Jan 28 '19 at 20:53
17

In flutter 1.20 :

DateTime now = DateTime.now();

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

Mohsen Emami
  • 2,709
  • 3
  • 33
  • 40
9

To get the current Time in AM/PM format.

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

RohanArihant
  • 2,560
  • 4
  • 19
  • 29
  • 2
    Your answer is really helpful just want to know one thing how to get the time in 24 hour format. – Roxx Oct 17 '20 at 15:13
7
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);
Iago G. Nunes
  • 71
  • 1
  • 4
3

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.

BusterT
  • 81
  • 1
0

pick a exact time in flutter app and put in local notification package...full detail guide.//(alarm App)enter image description here

Talha
  • 1
  • 2
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 10 '23 at 11:47