-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME
48 lines (32 loc) · 1.92 KB
/
README
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
TimeSpanExt is a .NET library which makes timespans useful and easy. The functionality of TimeSpanExt greatly exceeds that of the TimeSpan class that ships with .NET.
* Setup
1) Get the source:
git clone git://github.com/WizardOfOgz/TimeSpanExtensions.git
2) Build the TimeSpanExtensions project
3) Include the library in your project.
* Why use TimeSpanExt?
- The code looks nice: DateTime searchStartDate = 1.Month().Ago;
- Supports complex expressions: DateTime searchEndDate = searchStartDate + (1.Month() - 1.Day());
- Time spans can be stored and applied to more than one DateTime yielding the correct results:
class ScheduleEvent
{
public string Name { get; set; }
public DateTime StartAt { get; set; }
public TimeSpanExt Duration { get; set; }
public DateTime EndsAt {
get{ return StartTime + Duration; }
}
}
var vacationDuration = 1.Month();
var myVacation = new ScheduledEvent{ Name="One-month vacation", StartTime=DateTime.Parse("1/31/2011"), Duration=vacationDuration);
DateTime backToWork = myVacation.EndsAt; // Feb 28, 2011 - Wait that's only 28 days from the start of my vacation!
myVacation.StartTime = DateTime.Parse("3/30/2011"); // Change the start date. Duration is still one month.
DateTime backToWork = myVacation.EndsAt; // Apr 30, 2011 - Much better, taking off 31 days.
* Complex TimeSpanExts
TimeSpanExt also supports addition and subtraction. The order of the operands is preserved as is shown in this example:
var monthPlusDay = 1.Month() + 1.Day();
var dayPlusMonth = 1.Day() + 1.Month();
DateTime dt = DateTime.Parse("2/28/2010");
DateTime result;
result = dt + monthPlusDay; // returns 3/29/2010
result = dt + dayPlusMonth; // returns 4/1/2010