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()
- {
-
- double jd;
- double j, L, n;
- int d, m, y;
- int theDay;
-
- d = int.Parse(SolarDay.Text);
-
- m = SolarMonth.SelectedIndex + 1;
-
- 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;
- }
-
- if (jd < 1948440)
- {
- DateMinError();
- return;
- }
-
- if (jd > 2621734)
- {
- DateMaxError();
- return;
- }
-
- 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);
-
- 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";
- }
- }
Convert Lunar (Hijri) Date to Solar (Gregorian) Date
- private void LunarToSolar()
- {
-
- double jd;
- double i, j, k, L, n;
- int d, m, y;
- int theDay;
-
- d = int.Parse(LunarDay.Text);
- if (d == 1)
- {
- lblAge.Text = d.ToString() + " day";
- }
- else
- {
- lblAge.Text = d.ToString() + " days";
- }
-
- m = LunarMonth.SelectedIndex + 1;
-
- y = int.Parse(LunarYear.Text);
- jd = getInt((11 * y + 3) / 30) + 354 * y + 30 * m - getInt((m - 1) / 2) + d + 1948440 – 3
-
- 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);
- }
-
- SolarDay.Text = d.ToString();
- SolarMonth.Text = SolarMonths[m - 1];
- SolarYear.Text = y.ToString();
- ShowMoonPhase();
- }
Draw the Moon at Selected Date
- private void ShowMoonPhase()
- {
- int ag = int.Parse(LunarDay.Text);
- double Phase = ag / 29.530588853;
- int Xpos, Ypos, Rpos;
- int Xpos1, Xpos2;
- this.ClearDraw();
-
- int PageWidth = this.MoonShape.Width;
-
- int PageHeight = this.MoonShape.Height;
-
- Bitmap ImageToDraw = new Bitmap(PageWidth, PageHeight);
-
- Graphics newGraphics = Graphics.FromImage(ImageToDraw);
- Pen PenW = new Pen(Color.White);
- Pen PenB = new Pen(Color.Black);
- for (Ypos = 0; Ypos <=45; Ypos++)
- {
- Xpos = (int)(Math.Sqrt(45 * 45 - Ypos * Ypos));
-
- 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);
-
- 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);
- }
-
- 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);
- }
-
- this.MoonShape.Image = ImageToDraw;
-
- PenW.Dispose();
- PenB.Dispose();
- newGraphics.Dispose();
- ImageToDraw = null;
- }
You can go to the source file to read the code, if you have any idea or find another code to convert dates then please tell me:
Mostafa Kaisoun
[email protected]