-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy pathDateManipulationLib.cs
162 lines (124 loc) · 5.3 KB
/
DateManipulationLib.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace NJCODateManipulation
{
public class Date
{
// Decidi reduzir o código utilizando parâmetros opcionais, reduzi o código pela metade :)
// Acabei percebendo que tinha o processo de otimização, mas como estava fazendo e seguindo, acabei refatorando
// o código de antemão, então eu vou colocar a classe otimizada diretamente aqui, pois quero utilizar essa
// biblioteca futuramente
public static int DateDifferenceInDays(string startDate, string endDate = "")
{
if (endDate == "")
{
endDate = DateTime.Now.ToString();
}
TimeSpan difference = PrepareDate(startDate, endDate);
int days = difference.Days;
return days;
}
public static int DateDifferenceInDays(int startDay, int startMonth, int startYear, int endDay = 0, int endMonth = 0, int endYear = 0)
{
if (endDay == 0 || endMonth == 0 || endYear == 0)
{
endDay = DateTime.Now.Day;
endMonth = DateTime.Now.Month;
endYear = DateTime.Now.Year;
}
TimeSpan difference = PrepareDate(startYear, startMonth, startDay, endYear, endMonth, endDay);
int days = difference.Days;
return days;
}
public static double DateDifferenceInMonths(string startDate, string endDate = "")
{
if (endDate == "")
{
endDate = DateTime.Now.ToString();
}
TimeSpan difference = PrepareDate(startDate, endDate);
const double divisor = 30.436875;
int days = difference.Days;
double months = (days / divisor);
return Math.Round(months, 3, MidpointRounding.AwayFromZero);
}
public static double DateDifferenceInMonths(int startDay, int startMonth, int startYear, int endDay = 0, int endMonth = 0, int endYear = 0)
{
if (endDay == 0 || endMonth == 0 || endYear == 0)
{
endDay = DateTime.Now.Day;
endMonth = DateTime.Now.Month;
endYear = DateTime.Now.Year;
}
TimeSpan difference = PrepareDate(startYear, startMonth, startDay, endYear, endMonth, endDay);
const double divisor = 30.436875;
int days = difference.Days;
double months = (days / divisor);
return Math.Round(months, 3, MidpointRounding.AwayFromZero);
}
public static double DateDifferenceInYears(string startDate, string endDate = "")
{
if(endDate == "")
{
endDate = DateTime.Now.ToString();
}
TimeSpan difference = PrepareDate(startDate, endDate);
const double divisor = 365.2425;
int days = difference.Days;
double years = (days / divisor);
return Math.Round(years, 3, MidpointRounding.AwayFromZero);
}
public static double DateDifferenceInYears(int startDay, int startMonth, int startYear, int endDay = 0, int endMonth = 0, int endYear = 0)
{
if (endDay == 0 || endMonth == 0 || endYear == 0)
{
endDay = DateTime.Now.Day;
endMonth = DateTime.Now.Month;
endYear = DateTime.Now.Year;
}
TimeSpan difference = PrepareDate(startYear, startMonth, startDay, endYear, endMonth, endDay);
const double divisor = 365.2425;
int days = difference.Days;
double years = (days / divisor);
return Math.Round(years, 3, MidpointRounding.AwayFromZero);
}
public static TimeSpan PrepareDate(string startDate, string endDate)
{
DateTime parsedStartDate = DateTime.Parse(startDate);
DateTime parsedEndDate = DateTime.Parse(endDate);
TimeSpan difference = parsedEndDate - parsedStartDate;
return difference;
}
public static TimeSpan PrepareDate(int startYear, int startMonth, int startDay, int endYear, int endMonth, int endDay)
{
DateTime startDate = new DateTime(startYear, startMonth, startDay);
DateTime endDate = new DateTime(endYear, endMonth, endDay);
TimeSpan difference = endDate - startDate;
return difference;
}
public static DateTime AddDaysInDate(int daysToAdd, int day, int month, int year)
{
if (day == 0 || month == 0 || year == 0)
{
day = DateTime.Now.Day;
month = DateTime.Now.Month;
year = DateTime.Now.Year;
}
DateTime date = new DateTime(year, month, day);
return date.AddDays(daysToAdd);
}
public static DateTime AddDaysInDate(int daysToAdd, string inputDate)
{
DateTime parsedInputDate = DateTime.Parse(inputDate);
return parsedInputDate.AddDays(daysToAdd);
}
public static DateTime AddDaysInToday(int daysToAdd)
{
return DateTime.Now.AddDays(daysToAdd);
}
}
}