Flutter – Find the Number of Days Between Two Dates
Last Updated :
14 May, 2023
In this article, we will see how to get the difference between the two dates and display them in Flutter. Below is the step-by-step implementation.
How to Use?
final date1 = DateTime(2022, 4, 11);
final date2 = DateTime.now();
late var difference;
int daysBetween(DateTime from, DateTime to) {
from = DateTime(from.year, from.month, from.day);
to = DateTime(to.year, to.month, to.day);
return (to.difference(from).inHours / 24).round();
}
Step By Step Implementation
Step 1: Create a New Project in Android Studio
To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.
Step 2: Import the material package
Adding material package that gives us to use the important method and calls the runApp method in the main function that will call our application.
import 'package:flutter/material.dart';
void main() {
runApp(RunMyApp());
}
Step 3: Create a class RunMyApp which going to be stateless, because there are no changes needed. That further returns the MaterialApp widget which gives the material components to build the Flutter application.
class RunMyApp extends StatelessWidget {
const RunMyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(primarySwatch: Colors.green),
debugShowCheckedModeBanner: false,
home:
);
}
}
Step 4: Creating Scaffold Widget
Give the home property and there can be a scaffold widget that has the property of AppBar and body. AppBar allows us to give the title of AppBar, color, leading, and trailing icon.
home: Scaffold(
appBar: AppBar(
title: Text('Difference Between Two Dates'),
),
),
Step 5: Show the date1, date2, and the difference between these two dates
We can use the column widget to display the date1, date2, and difference of dates using the Text widget.
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('First Date - ${date1}'),
Text('Last Date - ${date2}'),
Text('Difference - ${difference} Days'),
],
),
),
Complete Code
Dart
import 'package:flutter/material.dart' ;
void main() {
runApp(RunMyApp());
}
class RunMyApp extends StatefulWidget {
RunMyApp({super.key});
@override
State<RunMyApp> createState() => _RunMyAppState();
}
class _RunMyAppState extends State<RunMyApp> {
final date1 = DateTime(2022, 4, 11);
final date2 = DateTime.now();
late var difference;
int daysBetween(DateTime from, DateTime to) {
from = DateTime(from.year, from.month, from.day);
to = DateTime(to.year, to.month, to.day);
return (to.difference(from).inHours / 24).round();
}
@override
void initState() {
final data = daysBetween(date1, date2);
setState(() {
difference = data;
});
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false ,
theme: ThemeData(primarySwatch: Colors.green),
home: Scaffold(
appBar: AppBar(
title: Text( 'Difference Between Two Dates' ),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text( 'First Date - ${date1}' ),
Text( 'Last Date - ${date2}' ),
Text( 'Difference - ${difference} Days' ),
],
),
),
),
);
}
}
|
Output
Here we get the output that shows the three texts date1, date2, and the differences between them.
Share your thoughts in the comments
Please Login to comment...