{

How to get the name of the days of the week in Dart| Flutter by Example


 get the name of the days of the week  in Dart

DateTime.weekday returns an integer number from 1 to 7, Monday represents 1, and Sunday represents 7.

void main() {
  DateTime date = DateTime.now();
  print(date); //2022-04-10 20:48:56.354 and Sunday

  print(date.weekday); // 7
}

How to print the week name such as Monday in Dart.

How to get the name of the days of a week for a date in Dart?

There is no direct way to get with the In-built DateTime class and methods to get the name of the week.

  • use intl dependency

    First, Add the intl dependency to pubspec.yaml

dev_dependencies:
    intl: any

Next, Import the intl package in the dart code file

import 'package:intl/intl.dart';

Create DateFormat object with format EEEE Call DateFormat format method with a given date. Here is an example program

import 'package:intl/intl.dart';

void main() {
  var date = DateTime.now();
  print(DateFormat('EEEE').format(date)); // Sunday

}
  • use jiffy dependency

    jiffy is a date and time utility library, First, Add the jiffy dependency to pubspec.yaml

dependencies:
  jiffy: ^5.0.0

Next, Import jiffy package in the dart code file

import 'package:jiffy/jiffy.dart';

Create a Jiffy object that returns the current date and time Call Jiffy.EEEE property that gives a week name. Here is an example program

import 'package:jiffy/jiffy.dart';

void main() {
  var now = Jiffy();
  print(now.EEEE);// Sunday
}

Conclusion

To summarize, Shows you multiple ways to get the week name for a given DateTime object in dart and flutter.

THE BEST NEWSLETTER ANYWHERE
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
Subscribe
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