How to get Last Day of a Week, month, and year in Dart| Flutter by Example

This tutorial shows you multiple ways to get the Last Day of a Month and Year in Dart and Flutter.

How to get the Last Day of a month in Dart and Flutter

The first way, Using Datetime.day property

Construct the DateTime object using the constructor with month always considered as value -1.

For example, Object using DateTime(2013, 3, 0); created with Feb 2013 data.

void main() {
  DateTime now = DateTime.now(); // March 2022

  var date = new DateTime(now.year, now.month + 1, 0);

  print(date.day); // 30
  var date1 = new DateTime(2013, 3, 0); // Feb 2013
  print(date1.day); // 28



The second way is using the jiffy library

jiffy is a dart dependency library that contains date and time utilities Add the dependency to pubspec.yaml

  jiffy: ^5.0.0

install the dependency using the dart pub update command

Jiffy() returns the current date endOf method returns the end date using Units provided,. Units is an Enum class for MILLISECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, YEAR

Jiffy endOf(Units units) 

Here is a program

import 'package:jiffy/jiffy.dart';

void main() {
  var lastDay = Jiffy().endOf(Units.MONTH);
  var lastDay1 = Jiffy("2022-02-01").endOf(Units.MONTH);


2022-04-30 23:59:59.999
2022-02-28 23:59:59.999

How to get Last Day of a Year in Dart/Flutter

Jiffy().endOf(Units.YEAR) returns the last date of an year.

import 'package:jiffy/jiffy.dart';

void main() {
  var lastDay = Jiffy().endOf(Units.YEAR);
  var lastDay1 = Jiffy("2022-02-01").endOf(Units.YEAR);


2022-12-31 23:59:59.999
2022-12-31 23:59:59.999

How to get the Last Day of a Current Week in Dart/Flutter

Jiffy().endOf(Units.WEEK) returns last day of an current week.

import 'package:jiffy/jiffy.dart';

void main() {
  var lastDay = Jiffy().endOf(Units.WEEK);
  var lastDay1 = Jiffy("2022-02-01").endOf(Units.WEEK);


2022-04-16 23:59:59.999
2022-02-05 23:59:59.999


To summarize,

Examples for returning on the last day of the Week, Month, or Year of a given date.

Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
You'll get a notification every time a post gets published here.

Related posts

Dart| Flutter How to: Read pubspec.yaml attributes (version) with examples

How to Create and build a Singleton Class Dart or Flutter example| Dart By Example

What is the difference between var and dynamic and final in dart?| Flutter By Example

How to: Check if Phone number is valid or not in Dart| Flutter By Example

Dart| Flutter How to read an image from a disk and resize a file

Dart/Flutter: How to write setter and getter fields or members variables in a class with Example

Dart| Flutter How to get extension name and MIME type of a file with example