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.

Date Converter in C#

Note: You can read Months by its name or by its number.

Code

Best Calculation to Get Number Approximation

  1. private double getInt(double fNumber)
  2. {
  3. double intPart;
  4. if (fNumber < -0.0000001)
  5. {
  6. intPart = Math.Ceiling(fNumber - 0.0000001);
  7. }
  8. else
  9. {
  10. intPart = Math.Floor(fNumber + 0.0000001);
  11. }
  12. return intPart;
  13. }

Convert Solar (Gregorian) Date to Lunar (Hijri) Date

  1. private void SolarToLunar()
  2. {
  3. //convert Solar year from 622 to 2500
  4. double jd;
  5. double j, L, n;
  6. int d, m, y;
  7. int theDay;
  8. //Solar day
  9. d = int.Parse(SolarDay.Text);
  10. //get the number of Solar month
  11. m = SolarMonth.SelectedIndex + 1;
  12. // Solar year
  13. y = int.Parse(SolarYear.Text);
  14. if ((y > 1582) || ((y == 1582) && (m > 10)) || ((y == 1582) && (m == 10) && (d > 14)))
  15. {
  16. 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;
  17. }
  18. else
  19. {
  20. jd = 367 * y - getInt((7 * (y + 5001 + getInt((m - 9) / 7))) / 4) + getInt((275 * m) / 9) + d + 1729777;
  21. }
  22. //Solar year >= 622
  23. if (jd < 1948440)
  24. {
  25. DateMinError();
  26. return;
  27. }
  28. //Solar year <= 2500
  29. if (jd > 2621734)
  30. {
  31. DateMaxError();
  32. return;
  33. }
  34. //day of the week
  35. theDay = (int)(jd%7);
  36. lblDay.Text = WeekDays[theDay];
  37. L = jd - 1948440 + 10632;
  38. n = getInt((L - 1) / 10631);
  39. L = L - 10631 * n + 354;
  40. j = (getInt((10985 - L) / 5316)) * (getInt((50 * L) / 17719)) + (getInt(L / 5670)) * (getInt((43 * L) / 15238));
  41. L = L - (getInt((30 - j) / 15)) * (getInt((17719 * j) / 50)) - (getInt(j / 16)) * (getInt((15238 * j) / 43)) + 29;
  42. m = (int)(getInt((24 * L) / 709));
  43. d = (int)(L - getInt((709 * m) / 24));
  44. y = (int)(30 * n + j - 30);
  45. //display Lunar date
  46. LunarDay.Text = d.ToString();
  47. LunarMonth.Text = LunarMonths[m - 1];
  48. LunarYear.Text = y.ToString();
  49. ShowMoonPhase();
  50. if (d == 1)
  51. {
  52. lblAge.Text = d.ToString() + " day";
  53. }
  54. else
  55. {
  56. lblAge.Text = d.ToString() + " days";
  57. }
  58. }
Convert Lunar (Hijri) Date to Solar (Gregorian) Date
  1. private void LunarToSolar()
  2. {
  3. //convert Lunar year from 1 to 1900
  4. double jd;
  5. double i, j, k, L, n;
  6. int d, m, y;
  7. int theDay;
  8. //Lunar day
  9. d = int.Parse(LunarDay.Text);
  10. if (d == 1)
  11. {
  12. lblAge.Text = d.ToString() + " day";
  13. }
  14. else
  15. {
  16. lblAge.Text = d.ToString() + " days";
  17. }
  18. //get the number of Lunar month
  19. m = LunarMonth.SelectedIndex + 1;
  20. //Lunar year
  21. y = int.Parse(LunarYear.Text);
  22. jd = getInt((11 * y + 3) / 30) + 354 * y + 30 * m - getInt((m - 1) / 2) + d + 1948440 – 3
  23. //day of the week
  24. theDay = (int)(jd%7);
  25. lblDay.Text = WeekDays[theDay];
  26. if (jd > 2299160)
  27. {
  28. L = jd + 68569;
  29. n = getInt((4 * L) / 146097);
  30. L = L - getInt((146097 * n + 3) / 4);
  31. i = getInt((4000 * (L + 1)) / 1461001);
  32. L = L - getInt((1461 * i) / 4) + 31;
  33. j = getInt((80 * L) / 2447);
  34. d = (int)(L - getInt((2447 * j) / 80));
  35. L = getInt(j / 11);
  36. m = (int)(j + 2 - 12 * L);
  37. y = (int)(100 * (n - 49) + i + L);
  38. }
  39. else
  40. {
  41. j = jd + 1402;
  42. k = getInt((j - 1) / 1461);
  43. L = j - 1461 * k;
  44. n = getInt((L - 1) / 365) - getInt(L / 1461);
  45. i = L - 365 * n + 30;
  46. j = getInt((80 * i) / 2447);
  47. d = (int)(i - getInt((2447 * j) / 80));
  48. i = getInt(j / 11);
  49. m = (int)(j + 2 - 12 * i);
  50. y = (int)(4 * k + n + i - 4716);
  51. }
  52. //display Solar date
  53. SolarDay.Text = d.ToString();
  54. SolarMonth.Text = SolarMonths[m - 1];
  55. SolarYear.Text = y.ToString();
  56. ShowMoonPhase();
  57. }
Draw the Moon at Selected Date
  1. private void ShowMoonPhase()
  2. {
  3. int ag = int.Parse(LunarDay.Text);
  4. double Phase = ag / 29.530588853;
  5. int Xpos, Ypos, Rpos;
  6. int Xpos1, Xpos2;
  7. this.ClearDraw(); //clear PicMoon PictureBox
  8. //Width of 'ImageToDraw' Object = Width of 'PicMoon' control
  9. int PageWidth = this.MoonShape.Width;
  10. //Height of 'ImageToDraw' Object = Height of 'PicMoon' control
  11. int PageHeight = this.MoonShape.Height;
  12. //Initiate 'ImageToDraw' Object with size = size of control 'PicMoon' control
  13. Bitmap ImageToDraw = new Bitmap(PageWidth, PageHeight);
  14. //Create graphics object for alteration.
  15. Graphics newGraphics = Graphics.FromImage(ImageToDraw);
  16. Pen PenW = new Pen(Color.White); //For lighted part of the moon
  17. Pen PenB = new Pen(Color.Black); //For darkness part of the moon
  18. for (Ypos = 0; Ypos <=45; Ypos++)
  19. {
  20. Xpos = (int)(Math.Sqrt(45 * 45 - Ypos * Ypos));
  21. //Draw darkness part of the moon
  22. Point pB1 = new Point(90 - Xpos, Ypos + 90);
  23. Point pB2 = new Point(Xpos + 90, Ypos + 90);
  24. Point pB3 = new Point(90 - Xpos, 90 - Ypos);
  25. Point pB4 = new Point(Xpos + 90, 90 - Ypos);
  26. newGraphics.DrawLine(PenW, pB1, pB2);
  27. newGraphics.DrawLine(PenW, pB3, pB4);
  28. //Determine the edges of the lighted part of the moon
  29. Rpos = 2 * Xpos;
  30. if (Phase < 0.5)
  31. {
  32. Xpos1 = -Xpos;
  33. Xpos2 = (int)(Rpos - 2 * Phase * Rpos - Xpos);
  34. }
  35. else
  36. {
  37. Xpos1 = Xpos;
  38. Xpos2 = (int)(Xpos - 2 * Phase * Rpos + Rpos);
  39. }
  40. //Draw the lighted part of the moon
  41. Point pW1 = new Point(Xpos1 + 90, 90 - Ypos);
  42. Point pW2 = new Point(Xpos2 + 90, 90 - Ypos);
  43. Point pW3 = new Point(Xpos1 + 90, Ypos + 90);
  44. Point pW4 = new Point(Xpos2 + 90, Ypos + 90);
  45. newGraphics.DrawLine(PenB, pW1, pW2);
  46. newGraphics.DrawLine(PenB, pW3, pW4);
  47. }
  48. //Display the bitmap in the picture box.
  49. this.MoonShape.Image = ImageToDraw;
  50. //Release graphics object
  51. PenW.Dispose();
  52. PenB.Dispose();
  53. newGraphics.Dispose();
  54. ImageToDraw = null;
  55. }
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]