In Flutter a inbuilt class is available for developers to get current time and date. The class name is DateTime. In today’s tutorial we are using the DateTime class to extract only current Time from mobile. The DateTime class works perfectly in both Android and iOS platform. So in this tutorial we would Flutter Get Current Date With Day Month Year Format Example Android iOS Tutorial. We would convert the date in DD:MM:YY format in current tutorial.
Contents in this project Flutter Get Current Date With Day Month Year Format Example Android iOS Tutorial:
1. Open your project’s main.dart file and import material.dart package.
1 |
import 'package:flutter/material.dart'; |
2. Create void main runApp() method and here we would call or main MyApp class. This is our main Root class.
1 |
void main() => runApp(MyApp()); |
3. Creating our main MyApp class extends with State StatelessWidget. In this class we are calling GetDate() class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: Text('Get Current Date in Flutter') ), body: Center( child: GetDate() ) ) ); } } |
4. Creating a class named as GetDate extends StatefulWidget. In this class we would make the createState() State method with _GetDateState() class name to enable mutable state management in given class tree.
1 2 3 4 5 |
class GetDate extends StatefulWidget { _GetDateState createState() => _GetDateState(); } |
5. Creating our main Child class named as _GetDateState extends State. This is our main class in which we would perform all the code to get current date in flutter.
1 2 3 4 |
class _GetDateState extends State<GetDate> { } |
6. Creating a variable named as finalDate with empty data. We would use this variable to store the final formatted date.
1 |
String finalDate = ''; |
7. Creating a function named as getCurrentDate(). In this function we would first get the current date using DateTime class object. Now we would parse the date and now finally we would extract the current date, month and year and store in formattedDate variable. Now we would use the State method to set the formattedDate variable value to finalDate variable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
getCurrentDate(){ var date = new DateTime.now().toString(); var dateParse = DateTime.parse(date); var formattedDate = "${dateParse.day}-${dateParse.month}-${dateParse.year}"; setState(() { finalDate = formattedDate.toString() ; }); } |
8. Creating Widget Build area in _GetDateState class. Now we would make 1 Text Widget and 1 Raised Button widget. We would set the current date in Text widget and call the getCurrentDate() function from Raised Button onPress event.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
Widget build(BuildContext context) { return Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Padding( padding: EdgeInsets.all(8.0), child : Text("Date = $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 Date in Flutter'), ), ], ), )); } |
9. Complete source code for main.dart file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
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 Date in Flutter') ), 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.day}-${dateParse.month}-${dateParse.year}"; 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("Date = $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 Date in Flutter'), ), ], ), )); } } |
Screenshots:

i want date like this 👇
27-04-2019
double figure month
how to do that????
I want date like the following
October 15, 2021
How to do it?