Macro to convert Gregorian calendar to other calendar (example: hijri)

For example Gregorian to Hijri : (see answer below)

This is example of the function that returns any date from Gregorian date:

Function ConvertDate( sDate As String, sCalendar As String, sLanguage As String ) As Date
	Dim oCalendar As Object
	Dim sDay, sMonth, sYear As String
	Dim oLocale As New
	UNIXEPOCH = CDate("1970-01-01")
	oLocale.Language = sLanguage
	oCalendar = CreateUnoService( "" )
	oCalendar.LoadCalendar( sCalendar, oLocale )	
	oCalendar.DateTime = CDate( sDate ) - UNIXEPOCH	
	sDay = oCalendar.GetDisplayString(, _ )
	sMonth = oCalendar.GetDisplayString(, _ ) 
	sYear = oCalendar.GetDisplayString(, _ )	
	ConvertDate = CDate( sYear  & "-" & sMonth & "-" & sDay )
End Function

If you want to convert a gregorian date to Islamic date (هجري):

Sub Main
	Dim GregorianDate, ConvertedDate As Date
	Dim sCalendar, sLanguage As String
	GregorianDate = DateValue("2020-06-07")
	sCalendar = "hijri"
	sLanguage = "ar"
	ConvertedDate = ConvertDate( GregorianDate, sCalendar, sLanguage )
	Print ConvertedDate ' displays: 16.10.1441 which is: ‫16 شوال 1441‬
End Sub

All you need to do is provide:
Gregorian date
the type of the calendar(*) like "hijri", "jewish", etc.
and the language code which consists of two letters according to ISO 639

To see what are all the available calendar to use for a language, call getAllCalendars with a locale of your choice.
For example, if I want to see the available calendars for Japanese:

Dim oCalendar As Object
Dim availableCal as Variant
Dim oLocale As New
oLocale.Language = "ja"

oCalendar = CreateUnoService( "" )
availableCal  = oCalendar.getAllCalendars( oLocale )

Print availableCal(0) & " , " & availableCal(1)  ' This displays: gregorian , gengou

Please correct any mistakes :slight_smile:

More informations here:

and here:

1 Like

Hi, What about converting Hijri to Gregorian date? Thanks