file_download

NTP Time Assistant for UWP

Portable Time Library

Portable Time Library is a portable .NET Class Library which implements extension methods and custom types to handle common operations concerning date and time. Its goal is to make the programming life with dates and time a little bit easier.

Month struct

The Month struct represents a month. It has extension methods to get the localized name of the month and the number of the month. A further extension method returns the length of the month in days with an optional leap day for February.

using PortableTimeLibrary.Extensions;
using PortableTimeLibrary.Time;

// get the name
Month.March.ShortName();
Month.March.Name();

// get the name for a specific culture
Month.March.ShortName(CultureInfo.GetCultureInfo("de"));
Month.March.Name(CultureInfo.GetCultureInfo("de"));

// get the number of the month
Month.March.Number();

// get the length of the month in days
Month.March.Length();

// get the length of February in a leap year
Month.February(true);

 

LocalDate struct

The immutable LocalDate struct represents a date. It supports convertions from and to the DateTime struct of the .NET standard API. Furthermore a fluent API allows calculations upon dates. Comparison operators are also provided. Last but not least it can tell you the first day of its week.

using PortableTimeLibrary.Extensions;
using PortableTimeLibrary.Time;

// create some dates
LocalDate.Today;
new LocalDate(2016, 4, 10);
new LocalDate(2016, Month.April, 10);

// do some calculations and comparisons
LocalDate localDate = LocalDate.Today.addYears(1).addMonths(-2).addDays(3);

if (localDate < today) { }
else if (localDate > today) { }
else if (localDate == today) { }
else if (localDate != today) { }

// get the date of the first of the week
//     the resulting date is Monday 2016-04-04 for European cultures
LocalDate(2016, Month.April, 10).FirstDayOfWeek();

// or tell the API that you want Sunday as the first day
//     this will be the same date (2016-04-10)
LocalDate(2016, Month.April, 10).FirstDayOfWeek(DayOfWeek.Sunday);

 

DateTime extensions

By using extension methods the DateTime struct of the .NET standard API can be converted into a pure date as a LocalDate. Like the LocalDate struct it can return the first day of its week. In addition it is now able to merge with the date or the time components of another DateTime instance with a single method call.

using PortableTimeLibrary.Extensions;
using PortableTimeLibrary.Time;

// convert to a LocalDate
LocalDate localDate = DateTime.Now.ToLocalDate();

// get the first day of the week
DateTime firstDay = DateTime.Now.FirstDayOfWeek();

// merge another date into now
DateTime dateTimeToMerge = new DateTime(2016, 1, 1, 23, 59, 58);
DateTime dateTime = DateTime.Now.MergeDate(dateTimeToMerge);

// merge another time into now
dateTime = DateTime.Now.MergeTime(dateTimeToMerge);

 

Have a look at the source code on GitHub to see more features and method overloads.

License

Portable Time Library for UWP is licensed under the Microsoft Public License.

Release notes

Portable Time Library v1.0.0.0
  • Dedicated struct LocalDate to represent dates
  • Enum Month to represent months and extension methods for it
  • Wrappers with an implementation relying on the .NET standard API
  • Conversion between DateTime and the structs of Portable Time Library