Home > OS >  Dart: previous and next weekday name
Dart: previous and next weekday name

Time:06-30

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?

CodePudding user response:

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.

CodePudding user response:

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
  • Related