Line data Source code
1 : // Copyright 2023 The terCAD team. All rights reserved.
2 : // Use of this source code is governed by a CC BY-NC-ND 4.0 license that can be found in the LICENSE file.
3 :
4 : import 'package:app_finance/_classes/herald/app_locale.dart';
5 : import 'package:intl/intl.dart';
6 :
7 : extension DateTimeExt on DateTime {
8 2 : DateTime getPreviousDay([int day = DateTime.monday]) {
9 4 : int daysToSubtract = weekday - day;
10 2 : if (daysToSubtract < 0) {
11 0 : daysToSubtract += 7;
12 : }
13 4 : return subtract(Duration(
14 : days: daysToSubtract,
15 2 : hours: hour,
16 2 : minutes: minute,
17 2 : seconds: second,
18 2 : milliseconds: millisecond,
19 2 : microseconds: microsecond,
20 : ));
21 : }
22 :
23 4 : DateTime getStartingDay([int startingDay = 1]) {
24 8 : if (startingDay <= day) {
25 12 : return DateTime(year, month, startingDay);
26 : } else {
27 4 : DateTime previousMonth = DateTime(year, month - 1);
28 2 : if (month == 1) {
29 3 : previousMonth = DateTime(year - 1, 12);
30 : }
31 3 : return DateTime(previousMonth.year, previousMonth.month, startingDay);
32 : }
33 : }
34 :
35 0 : DateTime getNextMonth() => DateTime(year, month + 1, day, hour, minute, second);
36 :
37 0 : DateTime getPreviousMonth(int monthDelta) => DateTime(year, month - monthDelta, day, hour, minute, second);
38 :
39 0 : DateTime getNextYear() => DateTime(year + 1, month, day, hour, minute, second);
40 :
41 1 : String yMEd() {
42 : DateFormat formatterDate;
43 : try {
44 1 : formatterDate = DateFormat.yMEd(AppLocale.code);
45 : } catch (e) {
46 1 : formatterDate = DateFormat.yMEd('en_US');
47 : }
48 1 : return formatterDate.format(this);
49 : }
50 :
51 0 : String yearMonth() {
52 : DateFormat formatterDate;
53 : try {
54 0 : formatterDate = DateFormat.yM(AppLocale.code);
55 : } catch (e) {
56 0 : formatterDate = DateFormat.yM('en_US');
57 : }
58 0 : return formatterDate.format(this);
59 : }
60 :
61 0 : String monthDay() {
62 : DateFormat formatterDate;
63 : try {
64 0 : formatterDate = DateFormat.MMMMd(AppLocale.code);
65 : } catch (e) {
66 0 : formatterDate = DateFormat.MMMMd('en_US');
67 : }
68 0 : return formatterDate.format(this);
69 : }
70 :
71 0 : String fullMonth() {
72 : DateFormat formatterDate;
73 : try {
74 0 : formatterDate = DateFormat.MMMM(AppLocale.code);
75 : } catch (e) {
76 0 : formatterDate = DateFormat.MMMM('en_US');
77 : }
78 0 : return formatterDate.format(this);
79 : }
80 :
81 0 : String fullMonthYear() {
82 : DateFormat formatterDate;
83 : try {
84 0 : formatterDate = DateFormat.yMMMM(AppLocale.code);
85 : } catch (e) {
86 0 : formatterDate = DateFormat.yMMMM('en_US');
87 : }
88 0 : return formatterDate.format(this);
89 : }
90 :
91 0 : String dateTime() {
92 : DateFormat formatterDate;
93 : DateFormat formatterTime;
94 : try {
95 0 : formatterDate = DateFormat.yMMMd(AppLocale.code);
96 0 : formatterTime = DateFormat.jms(AppLocale.code);
97 : } catch (e) {
98 0 : formatterDate = DateFormat.yMMMd('en_US');
99 0 : formatterTime = DateFormat.jms('en_US');
100 : }
101 0 : return '${formatterTime.format(this)}, ${formatterDate.format(this)}';
102 : }
103 : }
|