0

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?

2 Answers2

2

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.

Ivo
  • 18,659
  • 2
  • 23
  • 35
  • This is dangerous on the days that are 23 or 25 hours long (at DST shifts). See my videos https://youtu.be/usFSVUEadyo and https://youtu.be/LpoBYgzKVwU for details. – Randal Schwartz Jul 01 '22 at 03:38
0

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
Sadhik
  • 304
  • 1
  • 6