-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
FinalSpan.cs
42 lines (36 loc) · 1.09 KB
/
FinalSpan.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
using System;
using System.Collections.Generic;
using System.Text;
namespace moment.net
{
public class FinalSpan
{
private DateTime _dt;
private readonly DayOfWeek _dayOfWeek;
public FinalSpan(DateTime dateTime, DayOfWeek dayOfWeek)
{
_dt = dateTime;
_dayOfWeek = dayOfWeek;
}
public DateTime InMonth()
{
var m = _dt.Month;
// only loop through the last seven days of the month
_dt = new DateTime(_dt.Year, _dt.Month, (DateTime.DaysInMonth(_dt.Year, _dt.Month) - 7), 0, 0, 0, _dt.Kind);
while (_dt.Month == m)
{
if (_dt.DayOfWeek == _dayOfWeek)
{
return _dt;
}
_dt = _dt.AddDays(1);
}
return DateTime.MaxValue;
}
public DateTime InYear()
{
var dt = new DateTime(_dt.Year, 12, (DateTime.DaysInMonth(_dt.Year, 12) - 7), 0, 0, 0, _dt.Kind);
return new FinalSpan(dt, _dayOfWeek).InMonth();
}
}
}