How to get Current Time in Flutter?
Jun 14, 2022 . Admin
Hi friends,
In this post, we will learn How to get Current Time in Flutter?. I would like to show you get current timestamp in flutter. if you want to see example of how to get current time in flutter build then you are a right place.
This article goes in detailed on how to get current time in flutter code. follow bellow step for flutter get current locale.
I will give you simple Example of flutter get current time example by seconds
So let's see bellow example:
Step 1: Create Flutter ProjectFollow along with the setup, you will be creating an Flutter app.
$flutter create flutter_get_current_time_tutorial
Navigate to the project directory:
$cd flutter_get_current_time_tutorialStep 2: Main File
Create a main.dart file in the lib directory
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: Text('Get Current Time') ), body: Center( child: GetDate() ) ) ); } } class GetDate extends StatefulWidget { _GetDateState createState() => _GetDateState(); } class _GetDateState extends State<GetDate> { String finalDate = ''; getCurrentDate(){ var date = new DateTime.now().toString(); var dateParse = DateTime.parse(date); var formattedDate = "${dateParse.hour}:${dateParse.minute}:${dateParse.second}"; setState(() { finalDate = formattedDate.toString() ; }); } @override Widget build(BuildContext context) { return Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Padding( padding: EdgeInsets.all(8.0), child : Text("Time = $finalDate", style: TextStyle(fontSize: 20), textAlign: TextAlign.center,) ), RaisedButton( onPressed: getCurrentDate, color: Colors.green, textColor: Colors.white, padding: EdgeInsets.fromLTRB(10, 10, 10, 10), child: Text('Click Here To Get Current Time'), ), ], ), ) ); } }Run this Debug App Output :
I hope it will help you....