Hijri (Arabic) date to Gregorian date converter module .. (1 Viewer)

Is it usefull for you ?


  • Total voters
    5

hafeezksa

New member
Local time
Today, 16:54
Joined
May 8, 2013
Messages
1
Below given module is working and tested with Ms Access (2007-2010). Even you can use in Ms excel. If there is error write me on hafeezksa2(AT)hotmail.

Code:

'------------------------------------------
Option Compare Binary
Option Explicit
' Constants from olenls.h
Private Const LOCALE_ICALENDARTYPE = &H1009 ' /* type of calendar specifier */
Private Const CAL_HIJRI = 6 ' /* Hijri (Arabic Lunar) calendar */
Private Declare Function GetLocaleInfo Lib "kernel32" Alias "GetLocaleInfoA" (ByVal lcid As Long, ByVal LCTYPE As Long, lpData As Any, ByVal cchData As Integer) As Integer
Private Declare Function GetSystemDefaultLCID Lib "kernel32" () As Long
Private Declare Function GetUserDefaultLCID Lib "kernel32" () As Long
Private m_iHijri As Integer
'------------------------------------------------------------
' StDteGregOfStDteHijri
'------------------------------------------------------------
Public Function StDteGregOfStDteHijri(ByVal stDateHijri As String) As String
On Error Resume Next
Dim dte As Date

If Len(stDateHijri) > 0 Then
VBA.Calendar = vbCalHijri
dte = CDate(stDateHijri)
VBA.Calendar = vbCalGreg
StDteGregOfStDteHijri = CStr(dte)
End If

If Err.Number <> 0 Then StDteGregOfStDteHijri = stDateHijri
End Function
'------------------------------------------------------------
' StDteHijriOfStDteGreg
'------------------------------------------------------------
Public Function StDteHijriOfStDteGreg(ByVal stDateGreg As String) As String
On Error Resume Next
Dim dte As Date

If Len(stDateGreg) > 0 Then
dte = CDate(stDateGreg)
VBA.Calendar = vbCalHijri
StDteHijriOfStDteGreg = CStr(dte)
VBA.Calendar = vbCalGreg
End If

If Err.Number <> 0 Then StDteHijriOfStDteGreg = stDateGreg
End Function
'-----------------------------------------------------
' FHijriCalendar
'-----------------------------------------------------
Public Property Get FHijriCalendar() As Boolean
Dim stCal As String

If m_iHijri = 0 Then
stCal = StGetLocaleInfo(LOCALE_ICALENDARTYPE, False)
If (Val(stCal) = CAL_HIJRI) Then
m_iHijri = 1
Else
m_iHijri = 2
End If
End If

FHijriCalendar = (m_iHijri = 1)
End Property
'----------------------------------------------------------------------
' StGetLocaleInfo
'
' Gets Locale (international) info about current config
' See LOCALE constants at top of module for LCTYPE values
'----------------------------------------------------------------------
Public Function StGetLocaleInfo(LCTYPE As Long, Optional fUserDefault As Boolean = True) As String
Dim lcid As Long
Dim stBuff As String * 255

'Get current language ID
If fUserDefault Then
lcid = GetUserDefaultLCID()
Else
lcid = GetSystemDefaultLCID()
End If

'ask for the locale info
If (GetLocaleInfo(lcid, LCTYPE, ByVal stBuff, Len(stBuff)) > 0) Then
StGetLocaleInfo = StFromSz(stBuff)
End If
End Function
'------------------------------------------------------------
' StFromSz
'
' Find the first vbNullChar in a string, and return
' everything prior to that character. Extremely
' useful when combined with the Windows API function calls.
'------------------------------------------------------------
Public Function StFromSz(ByVal sz As String) As String

Dim ich As Integer

ich = InStr(sz, vbNullChar)
Select Case ich
' It's best to put the most likely case first.
Case Is > 1
' Found in the string, so return the portion
' up to the null character.
StFromSz = Left$(sz, ich - 1)
Case 0
' Not found at all, so just
' return the original value.
StFromSz = sz
Case 1
' Found at the first position, so return an empty string.
StFromSz = vbNullString
End Select
End Function
 

oldman1975

New member
Local time
Today, 06:54
Joined
Jun 14, 2013
Messages
1
salam alaicum,thank you for this code mr. hafeezksa, but i would greatly appreciate if can you please show a very simple example ms access Form that i can download using this code? i really need to use it in one of my ms access program, i don't have an idea on how to apply this code in ms access (in text boxes with event procedures), thanking you very much.
 

hmushtaq

New member
Local time
Today, 06:54
Joined
Jun 29, 2013
Messages
7
Copy all the above code, open the VBA editor in your Access Database (using Alt + F11 shortcut), select Insert from the menu bar, and select Module. Paste all the code in the newly opened window and save it once you are done copying.

You can now use the functions imported in your newly created module onto your forms where you require them. e.g. if you would like to use the function to convert the date from Hijri to Gregorian, simply call the function with its name i.e.

Dim strHijToGreg As String
strHijToGreg = StDteGregOfStDteHijri(>>supply your argument here<<).

The argument can be your text box containing the Hijri date, and you can pass it to the function as an argument.

Offcourse you will be calling the function from an event on the form where you would like the date to be converted.

I have tried my best to explain it to you in the least non-sensical way. Hope I have partly succeeded in doing so. :)
 

rabuayya

Registered User.
Local time
Today, 09:54
Joined
Aug 29, 2012
Messages
26
Hi Mushtaq,

I have tried to do that. But the computer reutrns a Syntax Error. Not sure what I could be doing wrong.

What date format should the argument of the formula be in ?

Is it possible to post a simple example of the usage of the formula?
Thanks
Rami
 

hmushtaq

New member
Local time
Today, 06:54
Joined
Jun 29, 2013
Messages
7
try this example database. if the problem still persists, you can send me what you are trying to do through your code. I may be able to figure out where you went wrong...
 

Attachments

  • Database4.accdb
    968 KB · Views: 1,739

rabuayya

Registered User.
Local time
Today, 09:54
Joined
Aug 29, 2012
Messages
26
Thanks Mushtaq. That did indeed work. I just copied exactly the code in your example into my database.
I noticed however, the Hijri date is off by two days, do you know if this can be adjusted somehow?
Thanks
Rami
 

hmushtaq

New member
Local time
Today, 06:54
Joined
Jun 29, 2013
Messages
7
Honeslty, I haven't been able to get something of the like you have asked for. There is normally a one-day error on some dates upon conversion. Two days is what I haven't experienced so far. Should be a max of one-day error. What is the hijri date you have that's producing this error?
 

rabuayya

Registered User.
Local time
Today, 09:54
Joined
Aug 29, 2012
Messages
26
I tried 1/9/1434. The equivalent should be 10/7/2013 but instead, it come out as 8/7/2013.

Thanks
Rami
 

tamangspace

Registered User.
Local time
Today, 16:54
Joined
Jul 15, 2012
Messages
37
I also stroke some errors like this. But 1/9/1434 should not be 10/7/2013, it should be 9/7/2013 and appears 8/7/2013. But 1/11/1434 should be 7/9/2013 but result appears 5/9/2013. I tested 1/3/1434 should be 13/1/2013 but appears 12/1/2013 and hijri months 5 , 7 , 8 , 9, and 12 also one day off. We Hijri Calender users would be so grateful if http://www.trigeminal.com give some ideas for this errors.
Thanks again for this site.
 

aattas

Registered User.
Local time
Today, 16:54
Joined
Dec 24, 2014
Messages
74
Hello, Im new here.

I was reading gratefully a response from "hmushtag" on the conversion of Hijri to Gregorian and I really thanked him for that. I know it is little too late but , I was just wondering how can I change the code to convert the Gregorian to Hijrian instead.

Thank you again
 

tamangspace

Registered User.
Local time
Today, 16:54
Joined
Jul 15, 2012
Messages
37
Aattas
I am using like this. You can customize as per your need.
Code:
strGregDate = stDteGregofstDteHijri(your_hijri_date_ field_Or_strHijriDate)

 strHijriDate = stDteHijriofstDteGreg(your_greg_date_field_Or_strGregDate)
 

aattas

Registered User.
Local time
Today, 16:54
Joined
Dec 24, 2014
Messages
74
Dear Tamangspace ,

Thanks for the response, but as im new in this, can you tell me where in the module these codes to be inserted.

Thanks again
 

tamangspace

Registered User.
Local time
Today, 16:54
Joined
Jul 15, 2012
Messages
37
Dear aattas
If your form has Gregorian Date field and Hijri Date field then you can add the following code to your Gregorian Date field on AfterUpdate event.
Code:
 Me.[YourHijriDateField] = stDteHijriOfstDteGreg([YourGregorianDateFieldName])
In my database:
Code:
 Me.DepartureHijri = stDteHijriOfstDteGreg([DepartureGreg])
Best of Luck
 

aattas

Registered User.
Local time
Today, 16:54
Joined
Dec 24, 2014
Messages
74
Hi friend,

I tried to do as you advised, I use the Afterupdate on the Gregorian field but it doesnt do anything to the Hijri field.

Am i missing something

Thanks again
 

tamangspace

Registered User.
Local time
Today, 16:54
Joined
Jul 15, 2012
Messages
37
aattas,

If you put the code in afterupdate event of Gregorian date field and if you have typed the field names in square [ ] brackets, then it should work.

What error message are you getting when you update your Gregorian Field?

Can you show your afterupdate code here?
 

aattas

Registered User.
Local time
Today, 16:54
Joined
Dec 24, 2014
Messages
74
Private Sub OSPexpirydateE_AfterUpdate()
Me.[OSPexpirydate] = StDteHijriOfStDteGreg([OSPexpirydateE])
End Sub

When i input the gregorian date it typed in arabic the same gregorian date for example:

Greg date : 1/1/2010 Hijri date: 1 muharram (محرم 2010
 

tamangspace

Registered User.
Local time
Today, 16:54
Joined
Jul 15, 2012
Messages
37
Code:
Private Sub OSPexpirydateE_AfterUpdate()
Me.[OSPexpirydate] = StDteHijriOfStDteGreg([OSPexpirydateE])
End Sub

When i input the gregorian date it typed in arabic the same gregorian date for example:

Greg date : 1/1/2010 Hijri date: 1 muharram (محرم 2010
You can't get the same field value converted in the same field. You need another field. If your field [OSPexpirydate] is for Gregorian date then you need another field for Hijri Date or vise versa. If not so then, if your field [OSPexpirydate] contains Gregorian date then
Your code should look like:
Code:
Me.[Your Hijri date field here] = StDteHijriOfStDteGreg([OSPexpirydateE])
or if your hijri Date should be in [OSPexpirydate], then your code should like this:

Code:
Private Sub YourGrogorianDateField_AfterUpdate()
 Me.[OSPexpirydate] = StDteHijriOfStDteGreg([Gregorian date field])
 End Sub

Good Luck
 

aattas

Registered User.
Local time
Today, 16:54
Joined
Dec 24, 2014
Messages
74
hi tamangspace,

sorry it took so long, i was on vacation.

I tried the way you mentioned again, sometimes it worked perfectly, but sometimes it came out weird date for example:

Hejri date : 16/03/1438 the conversion came out as 3/05/96

its like its going backward counting.

Can you help
 

aattas

Registered User.
Local time
Today, 16:54
Joined
Dec 24, 2014
Messages
74
Hi its me again.

I still need help in this issue.

Now when enter the greg date , the hijri says the same as i asked for a translation. for example:
gredate = 01/07/2017 hijridate= 01 rajab 2017

to solve in an awkward way , i have to change the year to 2016 or any number, the hijridate comes out ok. then i have to change back the correct year and the correct arabic hijri appeared.

Why is this happening? pls help
 

Users who are viewing this thread

Top Bottom