Flutter - Find the Number of Days Between Two Dates
Last Updated :
28 Apr, 2025
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 method that
// runs the RunMyApp class
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;
// function to get the differnce of dates in days
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() {
// calling the daysBetween method
final data = daysBetween(date1, date2);
setState(() {
// update the result
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}'),
// display the difference
Text('Difference - ${difference} Days'),
],
),
),
),
);
}
}
Output
Here we get the output that shows the three texts date1, date2, and the differences between them.
Similar Reads
Flutter - Set MIN and MAX Selectable Dates in DatePicker In Flutter, a DatePicker is a widget that provides a user interface for selecting dates. It allows users to choose a specific date from a calendar-like interface. Flutter doesn't have a built-in DatePicker widget, but you can create a date picker using the showDatePicker function, which displays a d
5 min read
Flutter - Convert Formatted String into DateTime Object In Dart, there is 1 datatype called DateTime which refers to date and time. It has multiple inbuilt functions which provide us with various details like current date and time. Today we will learn how to convert a formatted string into a DateTime object of dart. For our convenience, we are changing t
2 min read
Format Dates in Flutter The Dart in-built method, for formatting, dates in Flutter according to the requirements is very limited and restrictive. While dealing with dates it should be in human-readable format but unfortunately, there's no way of formatting dates in flutter unless you make use of a third-party package.In th
3 min read
Flutter - Countdown Timer The countdown timer app is about setting a time that moves in reverse order, as it shows the time left in the upcoming event. A countdown timer is an accurate timer that can be used for a website or blog to display the countdown to any special event, such as a birthday or anniversary. Step-by-Step I
3 min read
Flutter - DatePicker The flutter, syncfusion date range picker is a widget that is used to select single or multiple dates along with the range between two dates. The use of this library makes the navigation between dates, weeks, months, years, and even centuries simple and easy. The following are the key features of Da
3 min read
Simple Age Calculator App using Flutter Flutter SDK is an open-source software development kit for building beautiful UI which is natively compiled. In this article, we'll build a simple Flutter app that calculates a person's age based on their birthdate. To do this, we'll need to take input from the user, perform the calculations, and di
3 min read