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 0 : DateTime getPreviousDay([int day = DateTime.monday]) {
9 0 : int daysToSubtract = weekday - day;
10 0 : if (daysToSubtract < 0) {
11 0 : daysToSubtract += 7;
12 : }
13 0 : return subtract(Duration(
14 : days: daysToSubtract,
15 0 : hours: hour,
16 0 : minutes: minute,
17 0 : seconds: second,
18 0 : milliseconds: millisecond,
19 0 : microseconds: microsecond,
20 : ));
21 : }
22 :
23 1 : DateTime getStartingDay([int startingDay = 1]) {
24 2 : if (startingDay <= day) {
25 3 : return DateTime(year, month, startingDay);
26 : } else {
27 0 : DateTime previousMonth = DateTime(year, month - 1);
28 0 : if (month == 1) {
29 0 : previousMonth = DateTime(year - 1, 12);
30 : }
31 0 : 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 0 : formatterDate = DateFormat.yMEd('en_US');
47 : }
48 1 : return formatterDate.format(this);
49 : }
50 :
51 1 : String yearMonth() {
52 : DateFormat formatterDate;
53 : try {
54 1 : formatterDate = DateFormat.yM(AppLocale.code);
55 : } catch (e) {
56 0 : formatterDate = DateFormat.yM('en_US');
57 : }
58 1 : return formatterDate.format(this);
59 : }
60 :
61 1 : String monthDay() {
62 : DateFormat formatterDate;
63 : try {
64 1 : formatterDate = DateFormat.MMMMd(AppLocale.code);
65 : } catch (e) {
66 0 : formatterDate = DateFormat.MMMMd('en_US');
67 : }
68 1 : return formatterDate.format(this);
69 : }
70 :
71 1 : String fullMonth() {
72 : DateFormat formatterDate;
73 : try {
74 1 : formatterDate = DateFormat.MMMM(AppLocale.code);
75 : } catch (e) {
76 0 : formatterDate = DateFormat.MMMM('en_US');
77 : }
78 1 : 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 : }
|