Introduction
The main objective for this class is to convert between two calendars, the main advantage of this class is that it is very rich in overloaded function. So you can choose the best format you need.
Using the Code
At the beginning, I defined variable cur
of type System.Web.HttpContext
. This variable will show any error happen during function execution but you have to turn Trace
to true
(in the ASP.NET page) in order to see error.
After that, I defined arCulture
of type CultureInfo
.
CultureInfo
class represents information about specific culture, you have to specify this culture in the class constructor by giving the culture identifier. For example, "ar-SA
" stands for Arabic-Saudi Arabia.
Also I defined h
of type HijriCalendar
. This variable represents instance of HijriCalendar
class.
Now, how can I specify which calendar to use for a specific culture in case there is more than one calendar in one culture?
arCul.DateTimeFormat.Calendar=h;
Also, I declared an array allFormats
, which contains the main possible formats for passed date, you can add your formats as well.
Where this Array Works?
When I am converting the string
to DateTime
format, I need to provide formats to match the string
with the date.
DateTime tempDate=DateTime.ParseExact(hijri,allFormats,
arCul.DateTimeFormat,DateTimeStyles.AllowWhiteSpaces);
There are two main functions in this class which are involved in converting dates:
public string HijriToGreg(string hijri)
You have to pass the Hijri date in string
format, the returned string
is equivalent Gregorian date and it will be in this format "yyyyMMdd
", if you want another format, then use the overloaded function and pass the format as second parameter.
The second function is:
public string GregToHijri(string greg)
This function is the opposite of the above one. Also it is overloaded for format.
Now for the rest of the class, I added additional functions which I think it's important in date manipulation, I provided each function with a small and easy to understand brief.
Deployment
You have to add this class to ASP.NET C# web project in order to compile correctly, then make an instance for this class in your ASP.NET page:
Dates d=new Dates();
Good luck!
using System;
using System.Web;
using System.Diagnostics;
using System.Globalization;
using System.Data;
using System.Collections;
namespace Bahsas
{
public class Dates
{
private HttpContext cur;
private const int startGreg=1900;
private const int endGreg=2100;
private string[] allFormats={"yyyy/MM/dd","yyyy/M/d",
"dd/MM/yyyy","d/M/yyyy",
"dd/M/yyyy","d/MM/yyyy","yyyy-MM-dd",
"yyyy-M-d","dd-MM-yyyy","d-M-yyyy",
"dd-M-yyyy","d-MM-yyyy","yyyy MM dd",
"yyyy M d","dd MM yyyy","d M yyyy",
"dd M yyyy","d MM yyyy"};
private CultureInfo arCul;
private CultureInfo enCul;
private HijriCalendar h;
private GregorianCalendar g;
public Dates()
{
cur = HttpContext.Current;
arCul=new CultureInfo("ar-SA");
enCul=new CultureInfo("en-US");
h=new HijriCalendar();
g=new GregorianCalendar(GregorianCalendarTypes.USEnglish);
arCul.DateTimeFormat.Calendar=h;
}
public bool IsHijri(string hijri)
{
if (hijri.Length<=0)
{
cur.Trace.Warn("IsHijri Error: Date String is Empty");
return false;
}
try
{
DateTime tempDate=DateTime.ParseExact(hijri,allFormats,
arCul.DateTimeFormat,DateTimeStyles.AllowWhiteSpaces);
if (tempDate.Year>=startGreg && tempDate.Year<=endGreg)
return true;
else
return false;
}
catch (Exception ex)
{
cur.Trace.Warn("IsHijri Error :"+hijri.ToString()+"\n"+
ex.Message);
return false;
}
}
public bool IsGreg(string greg)
{
if (greg.Length<=0)
{
cur.Trace.Warn("IsGreg :Date String is Empty");
return false;
}
try
{
DateTime tempDate=DateTime.ParseExact(greg,allFormats,
enCul.DateTimeFormat,DateTimeStyles.AllowWhiteSpaces);
if (tempDate.Year>=startGreg && tempDate.Year<=endGreg)
return true;
else
return false;
}
catch (Exception ex)
{
cur.Trace.Warn("IsGreg Error :"+greg.ToString()+"\n"+ex.Message);
return false;
}
}
public string FormatHijri(string date ,string format)
{
if (date.Length<=0)
{
cur.Trace.Warn("Format :Date String is Empty");
return "";
}
try
{
DateTime tempDate=DateTime.ParseExact(date,
allFormats,arCul.DateTimeFormat,DateTimeStyles.AllowWhiteSpaces);
return tempDate.ToString(format,arCul.DateTimeFormat);
}
catch (Exception ex)
{
cur.Trace.Warn("Date :\n"+ex.Message);
return "";
}
}
public string FormatGreg(string date ,string format)
{
if (date.Length<=0)
{
cur.Trace.Warn("Format :Date String is Empty");
return "";
}
try
{
DateTime tempDate=DateTime.ParseExact(date,allFormats,
enCul.DateTimeFormat,DateTimeStyles.AllowWhiteSpaces);
return tempDate.ToString(format,enCul.DateTimeFormat);
}
catch (Exception ex)
{
cur.Trace.Warn("Date :\n"+ex.Message);
return "";
}
}
public string GDateNow()
{
try
{
return DateTime.Now.ToString("yyyy/MM/dd",enCul.DateTimeFormat);
}
catch (Exception ex)
{
cur.Trace.Warn("GDateNow :\n"+ex.Message);
return "";
}
}
public string GDateNow(string format)
{
try
{
return DateTime.Now.ToString(format,enCul.DateTimeFormat);
}
catch (Exception ex)
{
cur.Trace.Warn("GDateNow :\n"+ex.Message);
return "";
}
}
public string HDateNow()
{
try
{
return DateTime.Now.ToString("yyyy/MM/dd",arCul.DateTimeFormat);
}
catch (Exception ex)
{
cur.Trace.Warn("HDateNow :\n"+ex.Message);
return "";
}
}
public string HDateNow(string format)
{
try
{
return DateTime.Now.ToString(format,arCul.DateTimeFormat);
}
catch (Exception ex)
{
cur.Trace.Warn("HDateNow :\n"+ex.Message);
return "";
}
}
public string HijriToGreg(string hijri)
{
if (hijri.Length<=0)
{
cur.Trace.Warn("HijriToGreg :Date String is Empty");
return "";
}
try
{
DateTime tempDate=DateTime.ParseExact(hijri,allFormats,
arCul.DateTimeFormat,DateTimeStyles.AllowWhiteSpaces);
return tempDate.ToString("yyyy/MM/dd",enCul.DateTimeFormat);
}
catch (Exception ex)
{
cur.Trace.Warn("HijriToGreg :"+hijri.ToString()+"\n"+ex.Message);
return "";
}
}
public string HijriToGreg(string hijri,string format)
{
if (hijri.Length<=0)
{
cur.Trace.Warn("HijriToGreg :Date String is Empty");
return "";
}
try
{
DateTime tempDate=DateTime.ParseExact(hijri,
allFormats,arCul.DateTimeFormat,DateTimeStyles.AllowWhiteSpaces);
return tempDate.ToString(format,enCul.DateTimeFormat);
}
catch (Exception ex)
{
cur.Trace.Warn("HijriToGreg :"+hijri.ToString()+"\n"+ex.Message);
return "";
}
}
public string GregToHijri(string greg)
{
if (greg.Length<=0)
{
cur.Trace.Warn("GregToHijri :Date String is Empty");
return "";
}
try
{
DateTime tempDate=DateTime.ParseExact(greg,allFormats,
enCul.DateTimeFormat,DateTimeStyles.AllowWhiteSpaces);
return tempDate.ToString("yyyy/MM/dd",arCul.DateTimeFormat);
}
catch (Exception ex)
{
cur.Trace.Warn("GregToHijri :"+greg.ToString()+"\n"+ex.Message);
return "";
}
}
public string GregToHijri(string greg,string format)
{
if (greg.Length<=0)
{
cur.Trace.Warn("GregToHijri :Date String is Empty");
return "";
}
try
{
DateTime tempDate=DateTime.ParseExact(greg,allFormats,
enCul.DateTimeFormat,DateTimeStyles.AllowWhiteSpaces);
return tempDate.ToString(format,arCul.DateTimeFormat);
}
catch (Exception ex)
{
cur.Trace.Warn("GregToHijri :"+greg.ToString()+"\n"+ex.Message);
return "";
}
}
public string GTimeStamp()
{
return GDateNow("yyyyMMddHHmmss");
}
public string HTimeStamp()
{
return HDateNow("yyyyMMddHHmmss");
}
public int Compare(string d1,string d2)
{
try
{
DateTime date1=DateTime.ParseExact(d1,allFormats,
arCul.DateTimeFormat,DateTimeStyles.AllowWhiteSpaces);
DateTime date2=DateTime.ParseExact(d2,allFormats,
arCul.DateTimeFormat,DateTimeStyles.AllowWhiteSpaces);
return DateTime.Compare(date1,date2);
}
catch (Exception ex)
{
cur.Trace.Warn("Compare :"+"\n"+ex.Message);
return -1;
}
}
}
}
License
This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.
A list of licenses authors might use can be found here.