The TimeKit library is a C# library that provides a set of utility methods for working with dates and times. The library includes methods for parsing and formatting date strings, calculating the difference between two dates in years, months, or days, converting between time zones, and working with the Hijri and Gregorian calendars. Additionally, there are methods for calculating the number of business days between two dates, finding the next occurrence of a specified day of the week, rounding a date to the nearest specified time interval, and more.
$ dotnet add package TimeKitTimeKit is a C# library for working with date and time values. It provides a set of utility methods for performing common operations such as parsing and formatting dates, calculating the difference between two dates, and converting between time zones. The library also includes support for the Hijri calendar and provides methods for working with business days and weeks.
To use TimeKit in your C# project, you can install it via NuGet. Open the NuGet Package Manager Console and run the following command:
Install-Package TimeKit
Once you have installed the package, you can start using the library in your code by adding the following using directive:
using TimeKit;
Here are some examples of using TimeKit:
// Parse a date string into a DateTime object
DateTime date = TimeUtils.Parse("2022-01-01");
// Format a DateTime object as a string using a specified format string
string formattedDate = TimeUtils.Format(date, "yyyy-MM-dd");
// Calculate the number of days between two DateTime objects
int daysApart = TimeUtils.GetDaysBetween(date, DateTime.Now);
// Convert a DateTime object to a specified time zone
DateTime convertedDate = TimeUtils.ConvertToTimeZone(date, "Pacific Standard Time");
// Calculate the number of months between two DateTime objects
int monthsApart = TimeUtils.GetMonthsBetween(date, DateTime.Now);
// Calculate the number of years between two DateTime objects
int yearsApart = TimeUtils.GetYearsBetween(date, DateTime.Now);
// Convert a Hijri date to a Gregorian date
DateTime hijriDate = TimeUtils.HijriToGregorian(1444, 5, 15);
// Convert a Gregorian date to a Hijri date
(int hijriYear, int hijriMonth, int hijriDay) = TimeUtils.GregorianToHijri(date);
// Check if a given year is a leap year in the Hijri calendar
bool isLeapYear = TimeUtils.IsHijriLeapYear(1444);
// Calculate the number of business days between two DateTime objects
int businessDays = TimeUtils.GetBusinessDaysBetween(date, DateTime.Now);
// Calculate the next occurrence of a specified day of the week
DateTime nextMonday = TimeUtils.GetNextDayOfWeek(date, DayOfWeek.Monday);
// Round a DateTime object to the nearest specified time interval
DateTime roundedDate = TimeUtils.RoundToNearestInterval(date, TimeSpan.FromMinutes(15));
// Calculate the number of days in a given month and year
int daysInMonth = TimeUtils.GetDaysInMonth(2022, 2);
// Calculate the number of days in a given year
int daysInYear = TimeUtils.GetDaysInYear(2022);
// Calculate the week number of a given date using a specified culture
CultureInfo culture = new CultureInfo("en-US");
int weekNumber = TimeUtils.GetWeekNumber(date, culture);
// Calculate the number of weeks in a given year using a specified culture
int weeksInYear = TimeUtils.GetWeeksInYear(2022, culture);
// Check if a given year is a leap year in the Gregorian calendar
bool isGregorianLeapYear = TimeUtils.IsGregorianLeapYear(2022);
If you have found a bug or have a feature request, please open an issue on the GitHub repository. If you would like to contribute code, please fork the repository and submit a pull request.
TimeKit is released under the Apache License.