Datetime Extensions

This project is a merge of several common DateTime operations on the form of extensions to System.DateTime, including natural date difference text (precise and human rounded), holidays and working days calculations on several culture locales.

View the Project on GitHub kappy/DateTimeExtensions

Support

You can point out bugs, feature requests, submit pull requests on the project web site on GitHub

Contents

This library is built in a single assembly DateTimeExtensions.dll

License

This project is licensed under the Apache License, Version 2.0.
You can read the license details here

Sample

Check out a sample web site featuring some of DateTimeExtensions in here

Getting Started

DateTimeExtensions is avaiable on nuget
To install DateTime Extensions, run the following command in the Package Manager Console

PM> Install-Package DateTimeExtensions

Major Feautures

Working Days Calculations

In many businesses there's the concept of a working day. Either being used to calculate the estimated finishing date of a workflow or to return a phone call, some business process may use the working day concept to add or subtract days from a date excluding weekends and holidays.

Usage Example


using DateTimeExtensions;

[Test]
public void use_holidays_in_calulations() {
	DateTimeCultureInfo pt_ci = new DateTimeCultureInfo("pt-PT");
	DateTime startDate = new DateTime(2011, 4, 21);

	//21-04-2011 - start
	//22-04-2011 - holiday
	//23-04-2011 - saturday
	//24-04-2011 - sunday
	//25-04-2011 - holiday
	//26-04-2011 - end

	DateTime endDate = startDate.AddWorkingDays( 1, pt_ci);
	Assert.IsTrue(endDate == startDate.AddDays(5));
}

Export Holidays to calendar format

Do you wish to export your locale year holidays to Outlook? Or even provide your own custom calendar suited to your business?

[Test]
public void can_save_to_file() {
	var exporter = ExportHolidayFormatLocator.LocateByType(ExportType.OfficeHolidays);
	string tempfile = Path.GetTempFileName();
	using (var fileStream = File.Create(tempfile)) {
		using (var textwriter = new StreamWriter(fileStream)) {
			exporter.Export(new DateTimeCultureInfo("pt-PT"), 2012, textwriter);
			textwriter.Flush();
			Assert.IsTrue(fileStream.Length > 0);
		}
	}
	File.Delete(tempfile);
}

Date Difference To Natural Text

This extensions can compare two dates in natural language based on the current locale on current thread CultureInfo.
This comes in two flavors: Exact time or human rounded.


[Test]
public void can_tranlate_to_natural_text_rounded() {
	var fromTime = DateTime.Now;
	var toTime = fromTime.AddHours(2).AddMinutes(45);

	var naturalText = fromTime.ToNaturalText(toTime, true, foo_ci);

	Assert.IsNotNullOrEmpty(naturalText);
	Assert.AreEqual("3 hours", naturalText);
}


[Test]
public void can_tranlate_to_exact_natural_text() {
	var fromTime = DateTime.Now;
	var toTime = DateTime.Now.AddHours(2).AddMinutes(30);

	var naturalText = fromTime.ToExactNaturalText(toTime, foo_ci);

	Assert.IsNotNullOrEmpty(naturalText);
	Assert.AreEqual("2 hours, 30 minutes", naturalText);
}

General goto dates

Some general utility extensions are also provided:


[Test]
public void get_next_and_last_tuesday() {
	var a_saturday = new DateTime(2011, 8, 20);

	var nextTuesday = a_saturday.NextDayOfWeek(DayOfWeek.Tuesday);
	var lastTuesday = a_saturday.LastDayOfWeek(DayOfWeek.Tuesday);
	Assert.IsTrue((nextTuesday.DayOfWeek == DayOfWeek.Tuesday) && (nextTuesday == new DateTime(2011, 8, 23)));
	Assert.IsTrue((lastTuesday.DayOfWeek == DayOfWeek.Tuesday) && (lastTuesday == new DateTime(2011, 8, 16)));
}

[Test]
public void get_first_and_last_tuesday_of_august() {
	var a_saturday = new DateTime(2011, 8, 13);

	var firstTuesday = a_saturday.FirstDayOfWeekOfTheMonth(DayOfWeek.Tuesday);
	var lastTuesday = a_saturday.LastDayOfWeekOfTheMonth(DayOfWeek.Tuesday);
	Assert.IsTrue((firstTuesday.DayOfWeek == DayOfWeek.Tuesday) && (firstTuesday == new DateTime(2011, 8, 2)));
	Assert.IsTrue((lastTuesday.DayOfWeek == DayOfWeek.Tuesday) && (lastTuesday == new DateTime(2011, 8, 30)));
}

Thanks

keyboard-centric bug tracker continuous integration server