Reputation: 1052
I need to get the Date for the weekend in Flutter(Only Friday and Saturday) and the Date for the first day and the last day of the month
This is the approach I tried for the weekend but it doesn´t work:
var dateuntilfriday = DateTime.now();
getFriday() async {
if (dateuntilfriday.weekday == DateTime.friday) {
dateuntilfriday.add(Duration(days: 0));
print("1 Executed");
} else if (dateuntilfriday.weekday == DateTime.saturday) {
dateuntilfriday.subtract(Duration(days: 1));
print("2 Executed");
} else if (dateuntilfriday.weekday == DateTime.sunday) {
dateuntilfriday.subtract(Duration(days: 2));
print("3 Executed");
} else if (dateuntilfriday.weekday == DateTime.monday) {
dateuntilfriday.add(Duration(days: 4));
print("4 Executed");
} else if (dateuntilfriday.weekday == DateTime.tuesday) {
dateuntilfriday.add(Duration(days: 3));
print("5 Executed");
} else if (dateuntilfriday.weekday == DateTime.wednesday) {
dateuntilfriday.add(Duration(days: 2));
print("6 Executed");
} else if (dateuntilfriday.weekday == DateTime.thursday) {
dateuntilfriday.add(Duration(days: 1));
print("7 Executed");
} else {
print(
"and Error Occurent while getting the date for the next or last friday");
}
print(dateuntilfriday);
}
Upvotes: 1
Views: 2182
Reputation: 8383
I think this achieves what you are looking for:
DateTime weekdayOf(DateTime time, int weekday) => time.add(Duration(days: weekday - time.weekday));
DateTime fridayOf(DateTime time) => weekdayOf(time, 5);
DateTime saturdayOf(DateTime time) => weekdayOf(time, 6);
DateTime firstMonthDayOf(DateTime time) => DateTime(time.year, time.month, 1);
DateTime lastMonthDayOf(DateTime time) => DateTime(time.year, time.month + 1, 0);
I used a trick for the last day of the month. Day [0]
of next month is the last day of this month.
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
void main() {
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
home: HomePage(),
),
);
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final now = DateTime.now();
final df = DateFormat.yMMMMEEEEd();
return Scaffold(
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Today is ${df.format(now)}'),
const SizedBox(height: 16.0),
Text('Friday is ${df.format(fridayOf(now))}'),
const SizedBox(height: 16.0),
Text('Saturday is ${df.format(saturdayOf(now))}'),
const SizedBox(height: 16.0),
Text('First day of month is ${df.format(firstMonthDayOf(now))}'),
const SizedBox(height: 16.0),
Text('Last day of month is ${df.format(lastMonthDayOf(now))}'),
],
),
);
}
}
DateTime weekdayOf(DateTime time, int weekday) => time.add(Duration(days: weekday - time.weekday));
DateTime fridayOf(DateTime time) => weekdayOf(time, 5);
DateTime saturdayOf(DateTime time) => weekdayOf(time, 6);
DateTime firstMonthDayOf(DateTime time) => DateTime(time.year, time.month, 1);
DateTime lastMonthDayOf(DateTime time) => DateTime(time.year, time.month + 1, 0);
Upvotes: 6