I need This current weeks day name, with the date like (01/12/2020). I can select the data, so that I can use the data further.
Asked
Active
Viewed 660 times
0
-
see below: https://stackoverflow.com/questions/54371874/how-get-the-name-of-the-days-of-the-week-in-dart – Arash Mohammadi Sep 12 '20 at 04:32
-
1no is not, i need select option of 7 days. – david david Sep 12 '20 at 04:44
-
1What does "select option of 7 days" mean? Your question is very unclear. As stated, you want to take a date (e.g. January 12, 2020) and derive the weekday name ("Sunday"), which is what the cited question already answers. It sounds like you want something else? – jamesdlin Sep 12 '20 at 05:55
2 Answers
1
/////////////////////////////////////////////////////// BUILD YOUR OWN ///////////////////////////////////////////////
Add this to your package's pubspec.yaml file:
dependencies:
intl: ^0.16.1
Import the package in the dart file
import 'package:intl/intl.dart';
...
var dateString = '01/12/2020';
var date = DateFormat('d/M/yyyy').parse(dateString);
List<String> daysInWeek=['Monday', 'Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday',];
print(daysInWeek[date.weekday-1]);

Amon C
- 1,710
- 9
- 17
-
You seem to have forgotten the order of the days of the week. – Christopher Moore Sep 12 '20 at 18:25
-
-
This code does not work. This will consistently return the incorrect day of the week. – Christopher Moore Sep 13 '20 at 02:12
-
-
-
-
Try your own code for a few different dates. You're going to get the incorrect day of the week and occasionally range/out of bounds errors. – Christopher Moore Sep 13 '20 at 23:59
-
-
Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/221425/discussion-between-christopher-moore-and-amon-chowdhury). – Christopher Moore Sep 14 '20 at 00:06
-1
I think what you're wanting is the actual name of the day of the week. In that case, this should do the trick:
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
var day = DateTime.parse('2020-09-12').weekday;
print(days[day]); // prints "Saturday"
Try pasting this code in dartpad to see how it works

loganrussell48
- 1,656
- 3
- 15
- 23
-
-
Ah, yes. Sunday is supposed to be at index 7. The first index doesn't actually get used. Dart's `weekday` returns 1-7, not 0-6 as I thought. – loganrussell48 Sep 14 '20 at 02:15
-
So it *does* work. If you have the indices in the order according to the Dart documentation. I updated the answer to reflect that. – loganrussell48 Sep 14 '20 at 02:17
-
Sunday works just fine. I tested it. With the day you originally stated, today's date – loganrussell48 Sep 14 '20 at 02:20