Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How get the name of the days of the week in Dart

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

like image 472
Sami Issa Avatar asked Jan 25 '19 19:01

Sami Issa


People also ask

How do you get the first day of the week on Flutter?

If you're using Dart in Flutter, you can use the MaterialLocalizations class to get the index of the first day of the week (0 = Sunday, 6 = Saturday).

How do you get the current day in Flutter?

We can use the DateTime. now() function to get the current date in Flutter. You do not need to import anything to use the DateTime module. This is an in-built module of Dart and you can use its now() function to get the current date with time.


2 Answers

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

like image 200
diegoveloper Avatar answered Oct 05 '22 14:10

diegoveloper


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

like image 29
Rami Ibrahim Avatar answered Oct 05 '22 15:10

Rami Ibrahim