Introduction
The Lunar (Hijri) calendar is very important for Muslims as is the Solar (Gregorian) calendar important because the Lunar calendar was related to some elements of worship, so I looked at many sites on the internet to understand how to calculate the age of the moon in any given day. I found many sites offering various ways and I took what I found to provide results closer to the truth.
I've noticed that most sites agree on the expense of the Julian date but don't agree on how to calculate the age of the moon and found the difference among these sites to be up to one day and when the moon's age is 30 days, the result is zero in some sites.
In this program I calculate the approximate age of the moon in days and did not give attention to the parts of the day of the hours and minutes.
For the program to be more useful, I added a PictureBox control to display the lighted part of the moon and darkness part of the moon commensurate with the age of the moon.
There is a small probability of a one-day error for any calculation to convert a date.

Note: You can read Months by its name or by its number.
Code
Best Calculation to Get Number Approximation
- private double getInt(double fNumber)
- {
- double intPart;
- if (fNumber < -0.0000001)
- {
- intPart = Math.Ceiling(fNumber - 0.0000001);
- }
- else
- {
- intPart = Math.Floor(fNumber + 0.0000001);
- }
- return intPart;
- }
Convert Solar (Gregorian) Date to Lunar (Hijri) Date
- private void SolarToLunar()
- {
- //convert Solar year from 622 to 2500
- double jd;
- double j, L, n;
- int d, m, y;
- int theDay;
- //Solar day
- d = int.Parse(SolarDay.Text);
- //get the number of Solar month
- m = SolarMonth.SelectedIndex + 1;
- // Solar year
- y = int.Parse(SolarYear.Text);
- if ((y > 1582) || ((y == 1582) && (m > 10)) || ((y == 1582) && (m == 10) && (d > 14)))
- {
- jd = getInt((1461 * (y + 4800 + getInt((m - 14) / 12))) / 4) + getInt((367 * (m - 2 - 12 * (getInt((m - 14) / 12)))) / 12) - getInt((3 * (getInt((y + 4900 + getInt((m - 14) / 12)) / 100))) / 4) + d - 32075;
- }
- else
- {
- jd = 367 * y - getInt((7 * (y + 5001 + getInt((m - 9) / 7))) / 4) + getInt((275 * m) / 9) + d + 1729777;
- }
- //Solar year >= 622
- if (jd < 1948440)
- {
- DateMinError();
- return;
- }
- //Solar year <= 2500
- if (jd > 2621734)
- {
- DateMaxError();
- return;
- }
- //day of the week
- theDay = (int)(jd%7);
- lblDay.Text = WeekDays[theDay];
- L = jd - 1948440 + 10632;
- n = getInt((L - 1) / 10631);
- L = L - 10631 * n + 354;
- j = (getInt((10985 - L) / 5316)) * (getInt((50 * L) / 17719)) + (getInt(L / 5670)) * (getInt((43 * L) / 15238));
- L = L - (getInt((30 - j) / 15)) * (getInt((17719 * j) / 50)) - (getInt(j / 16)) * (getInt((15238 * j) / 43)) + 29;
- m = (int)(getInt((24 * L) / 709));
- d = (int)(L - getInt((709 * m) / 24));
- y = (int)(30 * n + j - 30);
- //display Lunar date
- LunarDay.Text = d.ToString();
- LunarMonth.Text = LunarMonths[m - 1];
- LunarYear.Text = y.ToString();
- ShowMoonPhase();
- if (d == 1)
- {
- lblAge.Text = d.ToString() + " day";
- }
- else
- {
- lblAge.Text = d.ToString() + " days";
- }
- }
- private void LunarToSolar()
- {
- //convert Lunar year from 1 to 1900
- double jd;
- double i, j, k, L, n;
- int d, m, y;
- int theDay;
- //Lunar day
- d = int.Parse(LunarDay.Text);
- if (d == 1)
- {
- lblAge.Text = d.ToString() + " day";
- }
- else
- {
- lblAge.Text = d.ToString() + " days";
- }
- //get the number of Lunar month
- m = LunarMonth.SelectedIndex + 1;
- //Lunar year
- y = int.Parse(LunarYear.Text);
- jd = getInt((11 * y + 3) / 30) + 354 * y + 30 * m - getInt((m - 1) / 2) + d + 1948440 – 3
- //day of the week
- theDay = (int)(jd%7);
- lblDay.Text = WeekDays[theDay];
- if (jd > 2299160)
- {
- L = jd + 68569;
- n = getInt((4 * L) / 146097);
- L = L - getInt((146097 * n + 3) / 4);
- i = getInt((4000 * (L + 1)) / 1461001);
- L = L - getInt((1461 * i) / 4) + 31;
- j = getInt((80 * L) / 2447);
- d = (int)(L - getInt((2447 * j) / 80));
- L = getInt(j / 11);
- m = (int)(j + 2 - 12 * L);
- y = (int)(100 * (n - 49) + i + L);
- }
- else
- {
- j = jd + 1402;
- k = getInt((j - 1) / 1461);
- L = j - 1461 * k;
- n = getInt((L - 1) / 365) - getInt(L / 1461);
- i = L - 365 * n + 30;
- j = getInt((80 * i) / 2447);
- d = (int)(i - getInt((2447 * j) / 80));
- i = getInt(j / 11);
- m = (int)(j + 2 - 12 * i);
- y = (int)(4 * k + n + i - 4716);
- }
- //display Solar date
- SolarDay.Text = d.ToString();
- SolarMonth.Text = SolarMonths[m - 1];
- SolarYear.Text = y.ToString();
- ShowMoonPhase();
- }
- private void ShowMoonPhase()
- {
- int ag = int.Parse(LunarDay.Text);
- double Phase = ag / 29.530588853;
- int Xpos, Ypos, Rpos;
- int Xpos1, Xpos2;
- this.ClearDraw(); //clear PicMoon PictureBox
- //Width of 'ImageToDraw' Object = Width of 'PicMoon' control
- int PageWidth = this.MoonShape.Width;
- //Height of 'ImageToDraw' Object = Height of 'PicMoon' control
- int PageHeight = this.MoonShape.Height;
- //Initiate 'ImageToDraw' Object with size = size of control 'PicMoon' control
- Bitmap ImageToDraw = new Bitmap(PageWidth, PageHeight);
- //Create graphics object for alteration.
- Graphics newGraphics = Graphics.FromImage(ImageToDraw);
- Pen PenW = new Pen(Color.White); //For lighted part of the moon
- Pen PenB = new Pen(Color.Black); //For darkness part of the moon
- for (Ypos = 0; Ypos <=45; Ypos++)
- {
- Xpos = (int)(Math.Sqrt(45 * 45 - Ypos * Ypos));
- //Draw darkness part of the moon
- Point pB1 = new Point(90 - Xpos, Ypos + 90);
- Point pB2 = new Point(Xpos + 90, Ypos + 90);
- Point pB3 = new Point(90 - Xpos, 90 - Ypos);
- Point pB4 = new Point(Xpos + 90, 90 - Ypos);
- newGraphics.DrawLine(PenW, pB1, pB2);
- newGraphics.DrawLine(PenW, pB3, pB4);
- //Determine the edges of the lighted part of the moon
- Rpos = 2 * Xpos;
- if (Phase < 0.5)
- {
- Xpos1 = -Xpos;
- Xpos2 = (int)(Rpos - 2 * Phase * Rpos - Xpos);
- }
- else
- {
- Xpos1 = Xpos;
- Xpos2 = (int)(Xpos - 2 * Phase * Rpos + Rpos);
- }
- //Draw the lighted part of the moon
- Point pW1 = new Point(Xpos1 + 90, 90 - Ypos);
- Point pW2 = new Point(Xpos2 + 90, 90 - Ypos);
- Point pW3 = new Point(Xpos1 + 90, Ypos + 90);
- Point pW4 = new Point(Xpos2 + 90, Ypos + 90);
- newGraphics.DrawLine(PenB, pW1, pW2);
- newGraphics.DrawLine(PenB, pW3, pW4);
- }
- //Display the bitmap in the picture box.
- this.MoonShape.Image = ImageToDraw;
- //Release graphics object
- PenW.Dispose();
- PenB.Dispose();
- newGraphics.Dispose();
- ImageToDraw = null;
- }
Mostafa Kaisoun
[email protected]

Mostafa KaisounPosted Aug 21, 2014, 1:19 AM
private void ShowMoonPhase() { int ag = int.Parse(LunarDay.Text); double Phase = ag / 29.530588853; int Xpos, Ypos, Rpos; int Xpos1, Xpos2; int Px, Py; Random randNum = new Random(); this.ClearDraw(); //clear PicMoon PictureBox //Width of 'ImageToDraw' Object = Width of 'PicMoon' control int PageWidth = this.MoonShape.Width; //Height of 'ImageToDraw' Object = Height of 'PicMoon' control int PageHeight = this.MoonShape.Height; //Initiate 'ImageToDraw' Object with size = size of control 'PicMoon' control Bitmap ImageToDraw = new Bitmap(PageWidth, PageHeight); //draw stars for (int i = 0; i < 100; i++) { // Randome Point: Px = randNum.Next(MoonShape.Width); Py = randNum.Next(MoonShape.Height); ImageToDraw.SetPixel(Px, Py, Color.White); } //Create graphics object for alteration. Graphics newGraphics = Graphics.FromImage(ImageToDraw); Pen PenW = new Pen(Color.White); //For lighted part of the moon Pen PenB = new Pen(Color.Black); //For darkness part of the moon for (Ypos = 0; Ypos <=45; Ypos++) { Xpos = (int)(Math.Sqrt(45 * 45 - Ypos * Ypos)); //Draw darkness part of the moon Point pB1 = new Point(90 - Xpos, Ypos + 90); Point pB2 = new Point(Xpos + 90, Ypos + 90); Point pB3 = new Point(90 - Xpos, 90 - Ypos); Point pB4 = new Point(Xpos + 90, 90 - Ypos); newGraphics.DrawLine(PenW, pB1, pB2); newGraphics.DrawLine(PenW, pB3, pB4); //Determine the edges of the lighted part of the moon Rpos = 2 * Xpos; if (Phase < 0.5) { Xpos1 = -Xpos; Xpos2 = (int)(Rpos - 2 * Phase * Rpos - Xpos); } else { Xpos1 = Xpos; Xpos2 = (int)(Xpos - 2 * Phase * Rpos + Rpos); } //Draw the lighted part of the moon Point pW1 = new Point(Xpos1 + 90, 90 - Ypos); Point pW2 = new Point(Xpos2 + 90, 90 - Ypos); Point pW3 = new Point(Xpos1 + 90, Ypos + 90); Point pW4 = new Point(Xpos2 + 90, Ypos + 90); newGraphics.DrawLine(PenB, pW1, pW2); newGraphics.DrawLine(PenB, pW3, pW4); } //Display the bitmap in the picture box. this.MoonShape.Image = ImageToDraw; //Release graphics object PenW.Dispose(); PenB.Dispose(); newGraphics.Dispose(); ImageToDraw = null; }
Mostafa KaisounPosted Aug 21, 2014, 1:16 AM
You can display stars around the Moon, just replace the code of "ShowMoonPhase()" with following code: