Introduction

The Lunar (Hijri) calendar is very important for Muslims as is the Solar (Gregorian) calendar is 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 between 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

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

Code

Best Calculation to Get Number Approximation

  1. Private Function getInt(ByRef fNumber As Double) As Double
  2. If fNumber < -0.0000001 Then
  3. getInt = Math.Ceiling(fNumber - 0.0000001)
  4. Else
  5. getInt = Math.Floor(fNumber + 0.0000001)
  6. End If
  7. End Function

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

  1. Private Sub SolarToLunar()
  2. ' convert Solar year from 622 to 2500
  3. Dim jd As Double
  4. Dim j, L, n As Double
  5. Dim d, m, y As Integer
  6. Dim theDay As Integer
  7. ' Solar day
  8. d = Val(SolarDay.Text)
  9. 'get the number of Solar month
  10. m = SolarMonth.SelectedIndex + 1
  11. ' Solar year
  12. y = Val(SolarYear.Text)
  13. If (y > 1582) Or ((y = 1582) And (m > 10)) Or ((y = 1582) And (m = 10) And (d > 14)) Then
  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
  15. Else
  16. jd = 367 * y - getInt((7 * (y + 5001 + getInt((m - 9) / 7))) / 4) + getInt((275 * m) / 9) + d + 1729777
  17. End If
  18. ' Solar year >= 622
  19. If jd < 1948440 Then
  20. DateMinError()
  21. Exit Sub
  22. End If
  23. ' Solar year <= 2500
  24. If jd > 2621734 Then
  25. DateMaxError()
  26. Exit Sub
  27. End If
  28. 'day of the week
  29. theDay = jd Mod 7
  30. lblDay.Text = WeekDays(theDay)
  31. L = jd - 1948440 + 10632
  32. n = getInt((L - 1) / 10631)
  33. L = L - 10631 * n + 354
  34. j = (getInt((10985 - L) / 5316)) * (getInt((50 * L) / 17719)) + (getInt(L / 5670)) * (getInt((43 * L) / 15238))
  35. L = L - (getInt((30 - j) / 15)) * (getInt((17719 * j) / 50)) - (getInt(j / 16)) * (getInt((15238 * j) / 43)) + 29
  36. m = Int(getInt((24 * L) / 709))
  37. d = Int(L - getInt((709 * m) / 24))
  38. y = Int(30 * n + j - 30)
  39. ' display Lunar date
  40. LunarDay.Text = Str(d)
  41. LunarMonth.Text = LunarMonths(m - 1)
  42. LunarYear.Text = Str(y)
  43. ShowMoonPhase()
  44. If d = 1 Then
  45. lblAge.Text = Str(d) & " day"
  46. Else
  47. lblAge.Text = Str(d) & " days"
  48. End If
  49. End Sub

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

  1. Private Sub LunarToSolar()
  2. ' convert Lunar year from 1 to 1900
  3. Dim jd As Double
  4. Dim i, j, k, L, n As Double
  5. Dim d, m, y As Integer
  6. Dim theDay As Integer
  7. ' Lunar day
  8. d = Val(LunarDay.Text)
  9. If d = 1 Then
  10. lblAge.Text = Str(d) & " day"
  11. Else
  12. lblAge.Text = Str(d) & " days"
  13. End If
  14. 'get the number of Lunar month
  15. m = LunarMonth.SelectedIndex + 1
  16. ' Lunar year
  17. y = Val(LunarYear.Text)
  18. jd = getInt((11 * y + 3) / 30) + 354 * y + 30 * m - getInt((m - 1) / 2) + d + 1948440 - 385
  19. 'day of the week
  20. theDay = jd Mod 7
  21. lblDay.Text = WeekDays(theDay)
  22. If jd > 2299160 Then
  23. L = jd + 68569
  24. n = getInt((4 * L) / 146097)
  25. L = L - getInt((146097 * n + 3) / 4)
  26. i = getInt((4000 * (L + 1)) / 1461001)
  27. L = L - getInt((1461 * i) / 4) + 31
  28. j = getInt((80 * L) / 2447)
  29. d = Int(L - getInt((2447 * j) / 80))
  30. L = getInt(j / 11)
  31. m = Int(j + 2 - 12 * L)
  32. y = Int(100 * (n - 49) + i + L)
  33. Else
  34. j = jd + 1402
  35. k = getInt((j - 1) / 1461)
  36. L = j - 1461 * k
  37. n = getInt((L - 1) / 365) - getInt(L / 1461)
  38. i = L - 365 * n + 30
  39. j = getInt((80 * i) / 2447)
  40. d = Int(i - getInt((2447 * j) / 80))
  41. i = getInt(j / 11)
  42. m = Int(j + 2 - 12 * i)
  43. y = Int(4 * k + n + i - 4716)
  44. End If
  45. ' display Solar date
  46. SolarDay.Text = Str(d)
  47. SolarMonth.Text = SolarMonths(m - 1)
  48. SolarYear.Text = Str(y)
  49. ShowMoonPhase()
  50. End Sub

Draw the Moon at Selected Date

  1. Private Sub ShowMoonPhase()
  2. Dim ag As Integer = Val(LunarDay.Text)
  3. Dim Phase As Double = ag / 29.530588853
  4. Dim Xpos, Ypos, Rpos As Integer
  5. Dim Xpos1, Xpos2 As Integer
  6. Me.ClearDraw() 'clear PicMoon PictureBox
  7. ' Width of 'ImageToDraw' Object = Width of 'PicMoon' control
  8. Dim PageWidth As Integer = Me.MoonShape.Width
  9. ' Height of 'ImageToDraw' Object = Height of 'PicMoon' control
  10. Dim PageHeight As Integer = Me.MoonShape.Height
  11. ' Initiate 'ImageToDraw' Object with size = size of control 'PicMoon' control
  12. Dim ImageToDraw As Bitmap = New Bitmap(PageWidth, PageHeight)
  13. 'Create graphics object for alteration.
  14. Dim newGraphics As Graphics = Graphics.FromImage(ImageToDraw)
  15. Dim PenW As Pen = New Pen(Color.White) ' For lighted part of the moon
  16. Dim PenB As Pen = New Pen(Color.Black) ' For darkness part of the moon
  17. For Ypos = 0 To 45
  18. Xpos = Int(Math.Sqrt(45 * 45 - Ypos * Ypos))
  19. ' Draw darkness part of the moon
  20. Dim pB1 As Point = New Point(90 - Xpos, Ypos + 90)
  21. Dim pB2 As Point = New Point(Xpos + 90, Ypos + 90)
  22. Dim pB3 As Point = New Point(90 - Xpos, 90 - Ypos)
  23. Dim pB4 As Point = New Point(Xpos + 90, 90 - Ypos)
  24. newGraphics.DrawLine(PenW, pB1, pB2)
  25. newGraphics.DrawLine(PenW, pB3, pB4)
  26. ' Determine the edges of the lighted part of the moon
  27. Rpos = 2 * Xpos
  28. If (Phase < 0.5) Then
  29. Xpos1 = -Xpos
  30. Xpos2 = Int(Rpos - 2 * Phase * Rpos - Xpos)
  31. Else
  32. Xpos1 = Xpos
  33. Xpos2 = Int(Xpos - 2 * Phase * Rpos + Rpos)
  34. End If
  35. ' Draw the lighted part of the moon
  36. Dim pW1 As Point = New Point(Xpos1 + 90, 90 - Ypos)
  37. Dim pW2 As Point = New Point(Xpos2 + 90, 90 - Ypos)
  38. Dim pW3 As Point = New Point(Xpos1 + 90, Ypos + 90)
  39. Dim pW4 As Point = New Point(Xpos2 + 90, Ypos + 90)
  40. newGraphics.DrawLine(PenB, pW1, pW2)
  41. newGraphics.DrawLine(PenB, pW3, pW4)
  42. Next
  43. ' Display the bitmap in the picture box.
  44. Me.MoonShape.Image = ImageToDraw
  45. ' Release graphics object
  46. PenW.Dispose()
  47. PenB.Dispose()
  48. newGraphics.Dispose()
  49. ImageToDraw = Nothing
  50. End Sub

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]