ARTICLE :
 

Algorithm to convert English date to Nepali date using C# .NET

by SBajra on June 24th, 2011

Introduction

This article illustrates how to convert from English date to Nepali date. This article is illustrated using C# .NET as a programming language. However, the algorithm could be implemented in any language.

Background

It has been always a challenge to convert English date to Nepali date and vice versa. Nepali date is based on "Bikram Sambat" a variation of lunar calendar date system. However, English calendar which we see in most of the computing system is based on solar calendar. So, converting from English date to Nepali date is not straightforward and simple. Determination of Nepali calendar is based on movement of the moon so it is not pre-defined calendar like solar English calendar. English calendar is pre-defined, there is always specific number of days in each calendar month. But it's not the same with Nepali calendar. Nepali calendar can have 32 total days in month which is not possible in English calendar. In computer system if you use 32 day then it will give you an exception as invalid date.

This article does not deal with all the complexity of Nepali calendar but provides a solution to easily convert English date to Nepali date.

Detail

One popular simple solution to convert from English date to Nepali date is to add 56 days, 8 months and 16 days to English date and you will get approximate Nepali date. But it is only approximate Nepali date. Sometimes its exact but most of the time it will be +/- 1 day.

So, to exactly convert English date to Nepali date, the only easy solution is to have some kind of date mapping database between English date and Nepali date. But if you use database, you are talking about external database that you need to manage and distribute with your application. The solution in this article also uses data mapping but to minimal limited extend within the application context. So, no external database is required.

First of all, get the English date you want to convert to Nepali date.
In this example, I am getting current DateTime and using C# .NET.
DateTime enDate = DateTime.Now;

Now, get the DayOfYear for this English date.
int enDayOfYear = enDate.DayOfYear;

DayOfYear will give you integer value of the day in the year expressed between 1 to 366. For example, DayOfYear for Jan 1 will be 1, for Feb 15 will be 46, and so on.

In this algorithm, we need to calculate Nepali Year, Nepali Month, Nepali Day and Nepali DaysInMonth for a given English date.

For this, we need to use some data mapping as I mentioned earlier. For the fast access to the data, this algorithm uses integer array as data store for each English "Year". There is no database required.

Structure of the Nepali DateData array:


You can easily get this data from easily available 90 years Nepali calendar in the market. So, you know the limitation of this algorithm, that this algorithm is based on data. So, there is no magic in this algorithm.

However, good part of this algorithm is, it does not require storing of all the Nepali date data but uses only few key Nepali date elements such as Nepali year for Jan 1, Nepali day for Jan 1, total days in each month starting Nepali month Poush for Jan 1 to next year Poush month for Dec 31. That's all. You just need total of 15 data elements to calculate exact Nepali date.

Now let's start with the actual calculation of English date to Nepali date.

Get Nepali DateData array for the given English date year.
For example, for the year 2011:
int[] npDateData = GetNepaliDateDataArray(enDate.Year);

Method GetNepaliDateDataArray() will return the DateData array for the Nepali date calculation based on English date year.
public static int[] GetNepaliDateDataArray(int year)
{
	switch (year)
	{
		case 2011:
			return new int[15] { 2067, 17, 30, 29, 30, 30, 31, 31, 32, 32, 31, 30, 30, 29, 30 };
		case 2012:
		..........
		..........
		default:
			throw new ArgumentOutOfRangeException(year.ToString(), "English year is out of range. Can not convert to Nepali date");
	}
}
Once we have Nepali DateData array, initialize the Nepali Year from the Nepali DateData array.
int npYear = npDateData[0];
Year is the first or 0 index in the Nepali DateData array.

Then, initialize the Nepali "month" to 9. We are initializing month to 9 because English date Jan 1 always fall in Nepali month Poush which is 9th month of Nepali calendar.
int npMonth = 9;

Initialize Nepali DaysInMonth from the Nepali DateData Array. We are initializing with total days in the month of Poush where Jan 1 falls. This is index 2 (3rd item) in the array.
int npDaysInMonth = npDateData[2];

After initialization, now we have to compute actual Nepali day, Nepali month, Nepali year and Nepali DaysInMonth.
For this, we will be using a temporary variable "npTempDays" to store sum of total number of days in each Nepali calendar month starting with the Nepali calendar month Poush but for the month of Poush, we will be counting days only from Jan 1 onwards. This will give us the total days in first month of Poush from Jan 1 to end of Poush.
int npTempDays = npDateData[2] - npDateData[1] + 1; 

The trick is, if the value of English date DayOfYear (which we defined earlier "enDayOfYear") is less than or equal to this temporary variable "npTempDays" then it means the English date fall in that particular Nepali calendar month. For example, if the English date is "01/13/2011" then the enDayOfYear=13, and for month of Poush npTempDays=14. So, according to our rule this English date will fall in the month of Poush because enDayOfYear is less or equal to npTempDays.

Now, we have to loop through each month to find the exact Nepali month, year, daysInMonth.
for (int i = 3; enDayOfYear > npTempDays; i++)
{
	npTempDays += npDateData[i];
	npDaysInMonth = npDateData[i];
	npMonth++;
	if (npMonth > 12)
	{
		npMonth -= 12;
		npYear++;
	}
}

After the loop is complete, we will be able to determine - Nepali month, Nepali year and Nepali daysInMonth for a given English date.

Now, we will be able to calculate the actual Nepali day for the given English date.
int npDay = npDaysInMonth - (npTempDays - enDayOfYear);

Finally, we got our Nepali Day, Nepali Month, Nepali Year and Nepali DaysInMonth for the given English date.

If we keep everything together, there are hardly 14 lines of core code to calculate the Nepali date using this algorithm in C# .NET. Of course, we need to store the data but for each English to Nepali date calculation we just need to loop through 15 data elements from DateData array.

Conclusion

This article uses very simple algorithm to calculate Nepali date from English date. Example is demonstrated using C#.NET. There are hardly 14 lines of codes required for the core algorithm plus the DateData array.

For working version of the code, please download the attached source code. Please feel free to provide any feedback or comments.

You can also write similar algorithm to convert from Nepali to English date as well, which I am not including in this article.
Happy coding !

Download

Click here to download source code

Author:

Profile Photo
SBajra
Date: June 24th, 2011

Comments:



Profile Photo
Commented by Biswo
on June 18th, 2012 at 08:32:14 EST
Thanks Brother for providing me the NepaliDate Converter.