This project is a merge of several common DateTime operations in the form of extensions to System.DateTime and System.DateTimeOffset, including natural date difference text (precise and human rounded), holidays and working days calculations on several culture locales.
Feedback will be much appreciated. You can check out a sample (WIP) project online on http://datetimeextensions.azurewebsites.net/
The following major features are currently implemented:
- Add or subtract Working days using locale holidays
- Support for Regional holidays (limited locales)
- Export Holidays to calendar format
- Dates Difference in Natural Time (localized)
- Time of day
- General "goto" dates
- Supports SourceLink for debugging
- Extensions to both DateTime and DateTimeOffset (the Date portion)
Working Days Calculations
These extensions for System.DateTime adds methods to make calculations based on working days.
A working day is defined in
IWorkingDayCultureInfo in two ways:
IsWorkingDay(DayOfWeek dayOfWeek) IsWorkingDay(DateTime date)
The first defines which day of the week is a working day (by default, working days are all
week days except weekends). The last does the same as the first, but it's also able to check
for any holiday. By default, no holidays are defined, unless there is a
implemented for the current thread
Avaiable CultureInfo implementations:
|en-NZ||en-GD (gd-GD, not really sure about this locale)|
If your culture is not listed here you can contribute it!!!
Fork me, implement it and send me the pull request, or just create an issue on the project github site.
Export Holidays to calendar format
This feature allows you to export the holidays from a
DateTimeCultureInfo (see above)
and export it to Microsoft Office Outlook.
IExportHolidaysFormat interface exposes one simple method for it:
void Export(DateTimeCultureInfo dateTimeCultureInfo, int year, TextWriter writer)
var exporter = ExportHolidayFormatLocator.LocateByType(ExportType.OfficeHolidays); exporter.Export(new WorkingDayCultureInfo("pt-PT"), 2012, textwriter);
Dates Diff in Natural Time
These extensions can compare two dates in natural language based on the current locale on
There are 2 API points for them:
fromDate.ToNaturalText(toTime, bool round = true) fromDate.ToExactNaturalText(toTime)
The first will return the most valuable time component with value > 0. The round flag will try to round the most significant time component based on the next least significant. Also, the round flag will round minutes and seconds to quarters after the first one.
Avaiable CultureInfo implementations:
Time of Day
These extensions allow easy parsing of time expressions and add the ability to check if a DateTime instance is after, before or inside a period.
bool IsBetween(this DateTime dateTime, Time startTime, Time endTime) bool IsBefore(this DateTime dateTime, Time time) bool IsAfter(this DateTime dateTime, Time time)
fromDate.FirstDayOfTheMonth() fromDate.LastDayOfTheMonth() fromDate.LastDayOfWeek(DayOfWeek) fromDate.NextDayOfWeek(DayOfWeek) fromDate.LastDayOfWeekOfTheMonth(DayOfWeek) fromDate.FirstDayOfWeekOfTheMonth(DayOfWeek) fromDate.GetDiff(DateTime toDate)
This library supports SourceLink. Just make sure you have a compatible Visual Studio version and the Just My Code is disabled on Debugging options
How to Contribute
Feel free to fork the project, work on your fork and send me the pull requests. You can also create issues with the features or changes that you think important.
Also, this repository is built with autocrlf = true.
When adding holidays resources names, plase prefix the culture specific holidays with the country name to avoid name colisison. Example: Portugal_FreedomDay
Things to improve
- @manuelbarbosa for making me this awesome logo
- @matkoch for helping me build the build script using Nuke and also building a video using this project
- Jetbrains for allowing me an open source license of their pretty cool suit