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

 get Last Day of a Week, month, and year in Dart

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-10-30 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

Perl How to: Find a Given Variable String is numeric or not

Dart/Flutter: Check if String is Empty, Null, or Blank example

How to generate Unique Id UUID in Dart or Flutter Programming| Dart or Flutterby Example

How to Sort List of numbers or String in ascending and descending in Dart or Flutter example

Dart Enum comparison operator| Enum compareByIndex example| Flutter By Example

Dart Example - Program to check Leap year or not

Dart Example to count the number of digits in a number| Decimals Count in Dart