Is there any way in dart to find next and previous weekday name? Such as, today is say Friday, so previous day was Thursday and next day will Saturday. Can I programmatically handle that?


Solution 1: Ivo

At the time I posted this it's Thursday where I live

For yesterday:

print(DateFormat("EEEE").format(DateTime.now().add(Duration(days: -1))));

output:

Wednesday

For tomorrow:

print(DateFormat("EEEE").format(DateTime.now().add(Duration(days: 1))));

Output:

Friday

The output might be different depending on your locale.

This needs

import 'package:intl/intl.dart';

to work.


Solution 2: Sadhik

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

dependencies:
  intl: ^latest version

and then import :

import 'package:intl/intl.dart';

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

print(DateFormat('EEEE').format(DateTime.now())); // prints Tuesday
print(DateFormat('EEEE').format(DateTime.now().subtract(Duration(days: 1))));// prints Monday
print(DateFormat('EEEE').format(DateTime.now().add(Duration(days: 1)))); // prints Wednesday