Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime Flutter

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

like image 892
Tom O'Sullivan Avatar asked Aug 05 '18 17:08

Tom O'Sullivan


People also ask

How do I add DateTime in Flutter?

add method Null safety Returns a new DateTime instance with duration added to this. final today = DateTime. now(); final fiftyDaysFromNow = today. add(const Duration(days: 50));

How do you get the time now Flutter?

How to Show Time Picker on TextField Tap and Get Formatted Time in Flutter. In this example, you are going to make a TextFiled or TextFormField, and whenever the user taps on that field, the time picker dialogue will appear.


4 Answers

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

like image 167
Peter Haddad Avatar answered Sep 26 '22 02:09

Peter Haddad


In flutter 1.20 :

DateTime now = DateTime.now();

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

like image 20
Mohsen Emami Avatar answered Sep 24 '22 02:09

Mohsen Emami


To get the current Time in AM/PM format.

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

like image 20
RohanArihant Avatar answered Sep 23 '22 02:09

RohanArihant


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);
like image 45
Iago G. Nunes Avatar answered Sep 24 '22 02:09

Iago G. Nunes