79

Somebody knows how can I extract from DateTime the name of the day of the week?

ej:

DateTime date = DateTime.now();
String dateFormat = DateFormat('dd-MM-yyyy hh:mm').format(date);

Result -> Friday

Sacha Arbonel
  • 254
  • 2
  • 11
Sami Issa
  • 1,695
  • 1
  • 18
  • 29

7 Answers7

182

Use 'EEEE' as a date pattern

 DateFormat('EEEE').format(date); /// e.g Thursday

Don't forget to import

import 'package:intl/intl.dart';

Check this for more info : https://pub.dev/documentation/intl/latest/intl/DateFormat-class.html

UmNyobe
  • 22,539
  • 9
  • 61
  • 90
diegoveloper
  • 93,875
  • 20
  • 236
  • 194
49

in your pubspec.yaml file, dependencies section. add intl dependency like so :

dependencies:
  intl: ^0.16.0 // <-- dependency added here, remember to remove this comment
  // other dependencies . remove this comment too

more about intl dependency here : intl

This package provides internationalization and localization facilities, including message translation, plurals and genders, date/number formatting and parsing, and bidirectional text.

and then at the top of your dart file import :

import 'package:intl/intl.dart';

now you can use DateFormat as you wish, here is an example :

var date = DateTime.now();
print(date.toString()); // prints something like 2019-12-10 10:02:22.287949
print(DateFormat('EEEE').format(date)); // prints Tuesday
print(DateFormat('EEEE, d MMM, yyyy').format(date)); // prints Tuesday, 10 Dec, 2019
print(DateFormat('h:mm a').format(date)); // prints 10:02 AM

there are many formats you can use with DateFormat, more details found here : https://api.flutter.dev/flutter/intl/DateFormat-class.html

hope this helps. Thank you

griffins
  • 7,079
  • 4
  • 29
  • 54
Rami Mohamed
  • 2,505
  • 3
  • 25
  • 33
47

You can just use .weekday method to find out the day . eg:

 DateTime date = DateTime.now();
 print("weekday is ${date.weekday}");

This will return the weekday number, for eg tuesday is 2 , as shown below

class DateTime implements Comparable<DateTime> {
      // Weekday constants that are returned by [weekday] method:
      static const int monday = 1;
      static const int tuesday = 2;
      static const int wednesday = 3;
      static const int thursday = 4;
      static const int friday = 5;
      static const int saturday = 6;
      static const int sunday = 7;
      static const int daysPerWeek = 7;
singhnikhil
  • 670
  • 5
  • 7
5

You can use the below method to achieve the desired result. The return statement can be changed according to your preference. (This is if you want to skip the use of the intl package)

String dateFormatter(DateTime date) {
  dynamic dayData =
      '{ "1" : "Mon", "2" : "Tue", "3" : "Wed", "4" : "Thur", "5" : "Fri", "6" : "Sat", "7" : "Sun" }';

  dynamic monthData =
      '{ "1" : "Jan", "2" : "Feb", "3" : "Mar", "4" : "Apr", "5" : "May", "6" : "June", "7" : "Jul", "8" : "Aug", "9" : "Sep", "10" : "Oct", "11" : "Nov", "12" : "Dec" }';

  return json.decode(dayData)['${date.weekday}'] +
      ", " +
      date.day.toString() +
      " " +
      json.decode(monthData)['${date.month}'] +
      " " +
      date.year.toString();
}

You can call the above method as

dateFormatter(DateTime.now())

The produces the result - Sat, 11 Apr 2020

aseef khan
  • 59
  • 1
  • 3
5

Extensions are good solutions. You can do it for weekday also. Before you use, call once DateTimeExtension to see getMonthString()

extension DateTimeExtension on DateTime {
  String getMonthString() {
    switch (month) {
      case 1:
        return 'Jan.';
      case 2:
        return 'Feb.';
      case 3:
        return 'Mar.';
      case 4:
        return 'Apr.';
      case 5:
        return 'May';
      case 6:
        return 'Jun.';
      case 7:
        return 'Jul.';
      case 8:
        return 'Aug.';
      case 9:
        return 'Sept.';
      case 10:
        return 'Oct.';
      case 11:
        return 'Nov.';
      case 12:
        return 'Dec.';
      default:
        return 'Err';
    }
  }
}
3

I think the best solution is to use intl package as seen in other answers, but if you want to code your own solution you can do something like:

const Map<int, String> weekdayName = {1: "Monday", 2: "Tuesday", 3: "Wednesday", 4: "Thursday", 5: "Friday", 6: "Saturday", 7: "Sunday"};
print(weekdayName[DateTime.now().weekday]);

or you can extend DateTime class, doing something like:

extension DateTimeExtension on DateTime {
  String? weekdayName() {
    const Map<int, String> weekdayName = {1: "Monday", 2: "Tuesday", 3: "Wednesday", 4: "Thursday", 5: "Friday", 6: "Saturday", 7: "Sunday"};
    return weekdayName[weekday];
  }
}
print(DateTime.now().weekdayName());

you can test these solutions in DartPad1 and DartPad2

Paulo Belo
  • 3,759
  • 1
  • 22
  • 20
1

If you want to get the localised name here is the bulletproof solution for a dedicated weekday.

You need to be a bit tricky.

Let's design the API:

String getWeekdayName(int weekday)

We surely know that we can get the weekday from DateTime:

DateTime.weekday // returns ISO value

It returns an ISO value (Monday = 1, Sunday = 7)

We also know that to get the "Monday", "Tuesday" (or any localised value) we can do:

DateFormat('EEEE').format(date);  // returns String

So how to get the name for int weekday?

We should create DateTime somehow with weekday and then format it accordingly.

We must also commit to passing an ISO-compliant value for the weekday, which is in <1,7> range.

Solution:

String getWeekdayName(int weekday) {
    final DateTime now = DateTime.now().toLocal();
    final int diff = now.weekday - weekday; // weekday is our 1-7 ISO value
    var udpatedDt;
    if (diff > 0) {
        udpatedDt = now.subtract(Duration(days: diff));
    } else if (diff == 0) {
        udpatedDt = now;
    } else {
        udpatedDt = now.add(Duration(days: diff * -1));
    }
    final String weekdayName = DateFormat('EEEE').format(udpatedDt);
    return weekdayName;
}
Michał Dobi Dobrzański
  • 1,449
  • 1
  • 20
  • 19