Do somthings

DoSomthings logo

Hii guys today we learn how to get the current date and time in a flutter. to get the Current date and time we use the DateTime.now(); function on flutter. You don’t need to import any dart package. this is an in-build module of dart and you can use its now() function. But in this article, we see how to get the current date and time in flutter in the correct format by using intl | Dart Package.

The most important library is intl. It defines the Intl class, with the default locale and methods for accessing most of the internationalization mechanisms. This library also defines the DateFormatNumberFormat, and BidiFormatter classes.

Step 1: First we get Time using the flutter in-built module.

import 'package:flutter/material.dart';

class DateAndTime extends StatefulWidget {
const DateAndTime({Key? key}) : super(key: key);

@override
State<DateAndTime> createState() => _DateAndTimeState();
}

class _DateAndTimeState extends State<DateAndTime> {
@override
Widget build(BuildContext context) {
String datetime = DateTime.now().toString();

return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text(
'Date & Time',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
),
body: Center(
child: Text(
datetime,
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Color(0xFF319964)),
textAlign: TextAlign.center,
),
),
);
}
}

you see in the image we get the current date and time in flutter in-build module but not in the correct format. don’t worry we use intl | Dart Package. see how to import the package.

Step 2: import intl dart package in your project to get current date and time in flutter


dependencies:
intl: ^0.17.0

Step 3: Implement this code in your project

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

class DateAndTime extends StatefulWidget {
const DateAndTime({Key? key}) : super(key: key);

@override
State<DateAndTime> createState() => _DateAndTimeState();
}

class _DateAndTimeState extends State<DateAndTime> {
@override
Widget build(BuildContext context) {
var now = DateTime.now();
var formatterDate = DateFormat('dd/MM/yy');
String actualDate = formatterDate.format(now);
var formatterTime = DateFormat('hh:mm');
String actualTime = formatterTime.format(now);

return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text(
'Date & Time',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
actualDate,
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Color(0xFF319964)),
),
SizedBox(
height: 30,
),
Text(
actualTime,
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Color(0xFF319964)),
),
],
),
),
);
}
}

Conclusion

  • Thanks for reading !!!
  • In this article, we learn how to get the current time and date in a flutter in two ways.
  • the first way is to use the flutter in-build function like the DateTime.now() function.
  • and second-way import intl package on your project and use like formatterDate.format(now); 

Do let us know if you need any assistance with flutter development. We would love to assist you. and also if you want another interesting article like; how to implement Day Night Time Picker in Flutter, how to check internet connection in a flutter, How to create Onboarding Screen Sliders on Flutter. so please connect with us. 

Categories: Flutter