There’s a new date calculation in Farsi Library that might come in handy. The new function was added a few days ago and calculates date/time differences in a user friendly manner.

Git Commit

The usage is pretty straightforward. Take the following test as an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[TestCase(-10, "10 minutes ago", "en-US")]
[TestCase(10, "10 minutes from now", "en-US")]
[TestCase(10, "ده دقیقه بعد", "fa-IR")]
[TestCase(-10, "ده دقیقه قبل", "fa-IR")]
public void Can_Format_Minutes(int minutes, string expected, string cultureName)
{
using (var context = new CultureSwitchContext(new CultureInfo(cultureName)))
{
var date = DateTime.Now.AddMinutes(minutes);
var pretty = new PrettyTime();

var result = pretty.Format(date);

Assert.AreEqual(expected, result);
}
}

Some of the features of this function:

  • It works for dates in future as well as dates in past
  • Works with Gregorian Calendar as well as Persian Calendar
  • Supports localization for fa-IR and neutral cultures out of the box.

The conversion works with different time units as small as Seconds and Minutes up to Decade, Centuries and Milleniums. It can optionally work with a reference date other than DateTime.Now so you can easily calculate any two date differences.

Originally FarsiLibrary project was started to facilitate working with Persian Calendar by providing date calculation functions and controls for Winform/WPF. The project was started back in .NET 1.1 timeframe with no PersianCalendar built-in the .NET Framework but even now, with multi-culture support for the controls and integration with other 3rd party controls, it solves many problems rather elegantly, even if I say so myself. It is an open-source project under MIT license.

This will be released in FarsiLibrary v3.0 but you can already take it for a spin and give me some feedback.