Introduction
The main objective for this class is to convert between two calendars(mainly arabic and english calendar), the main advantage of this class is that it is very rich in overloaded function. So you can choose the best format you need.
Background
First, I have to thank Anas Bahsas for his article, Convert Date from Hijri Calendar to Gregorian Calendar and Vice Versa.
Using the code
At the beginning, 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 e.g., "ar-SA
" stands for Arabic-Saudi Arabia.
Also I defined h
of type HijriCalendar
. This variable represents instance of HijriCalendar
class
CultureInfo arCI = new CultureInfo("ar-SA");
Also, I declared an single Format
,if u want to get All formats, which contains the main possible formats for passed date, you can add your formats as well use this code all format array
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"};
here I am using single format dd/mm/yyyy
DateTime tempDate = DateTime.ParseExact(hijri, "dd/MM/yyyy hh:mm:ss", arCI.DateTimeFormat, DateTimeStyles.AllowInnerWhite);
using step to step by process with simple code
Step 1
Go to the VS 2010 Menu and select "File" -> "New" -> "Project..." then select Visual C# -> Window Forms Application and create the Windows Forms application. Fig.1
Step 2
Then we will select the make Form Fig 2 using Textbox, Label, Button fig.2
Step 3
Then go to the Form1 file and write this code to convert to Arabic date to English date
Double click BUTTON write this code fig 3
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Globalization;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
CultureInfo arCI = new CultureInfo("ar-SA");
string hijri = textBox1.Text + " 00:00:00";
DateTime tempDate = DateTime.ParseExact(hijri, "dd/MM/yyyy hh:mm:ss", arCI.DateTimeFormat, DateTimeStyles.AllowInnerWhite);
string ar = tempDate.ToString(new CultureInfo("ar-SA"));
GregorianCalendar enCalendar = new GregorianCalendar();
int year = enCalendar.GetYear(tempDate);
int month = enCalendar.GetMonth(tempDate);
int day = enCalendar.GetDayOfMonth(tempDate);
textBox2.Text = (string.Format("{0}/{1}/{2}", day, month, year));
}
}
}
Step 4: Then we will Enter the valid arabic date (hijiri date) in the format of "DD/MM/YYYY" if u want change the format u can include format also. fig4
output
Points of Interest
Working as Jr. Software Developer at Alvaro Solution
Interested In C# based Windows,Web,Mobile Apps Development